如何索引函数返回的 MATLAB 数组而不先将其分配给局部变量?
例如,如果我想从 magic(5)
读取中间值,我可以这样做:
M = magic(5);
value = M(3,3);
获取 value == 13
。我希望能够执行以下操作之一:
value = magic(5)(3,3);
value = (magic(5))(3,3);
无需中间变量。但是,MATLAB 会抱怨 3
之前的第一个括号上存在 不平衡或意外的括号或方括号
。
是否可以从数组/矩阵中读取值而不先将其分配给变量?
For example, if I want to read the middle value from magic(5)
, I can do so like this:
M = magic(5);
value = M(3,3);
to get value == 13
. I'd like to be able to do something like one of these:
value = magic(5)(3,3);
value = (magic(5))(3,3);
to dispense with the intermediate variable. However, MATLAB complains about Unbalanced or unexpected parenthesis or bracket
on the first parenthesis before the 3
.
Is it possible to read values from an array/matrix without first assigning it to a variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
实际上可以做你想做的事,但是你必须使用索引运算符的函数形式。当您使用
()
执行索引操作时,您实际上是在调用subsref
函数。因此,即使您不能这样做:您可以这样做:
丑陋,但可能。 ;)
一般来说,您只需将索引步骤更改为函数调用,这样就不会出现紧随其后的两组括号。另一种方法是定义您自己的匿名函数进行下标索引。例如:
然而,总而言之,临时局部变量解决方案的可读性要高得多,这绝对是我所建议的。
It actually is possible to do what you want, but you have to use the functional form of the indexing operator. When you perform an indexing operation using
()
, you are actually making a call to thesubsref
function. So, even though you can't do this:You can do this:
Ugly, but possible. ;)
In general, you just have to change the indexing step to a function call so you don't have two sets of parentheses immediately following one another. Another way to do this would be to define your own anonymous function to do the subscripted indexing. For example:
However, when all is said and done the temporary local variable solution is much more readable, and definitely what I would suggest.
只有 好博文,关于 Loren 谈 Matlab 艺术 几天之前有一些可能有帮助的宝石。特别是,使用像这样的辅助函数:
where
paren()
can be use likewill return
我也猜测这会比 gnovice 的答案更快,但我还没有检查过(使用探查器! !)。话虽如此,您还必须在某处包含这些函数定义。我个人在我的道路上使它们成为独立的功能,因为它们非常有用。
这些函数和其他函数现在可在函数式编程构造附加组件中使用,该附加组件可通过 MATLAB 附加组件资源管理器或在 文件交换。
There was just good blog post on Loren on the Art of Matlab a couple days ago with a couple gems that might help. In particular, using helper functions like:
where
paren()
can be used likewould return
I would also surmise that this will be faster than gnovice's answer, but I haven't checked (Use the profiler!!!). That being said, you also have to include these function definitions somewhere. I personally have made them independent functions in my path, because they are super useful.
These functions and others are now available in the Functional Programming Constructs add-on which is available through the MATLAB Add-On Explorer or on the File Exchange.
您对使用未记录的功能有何感受:
或对于单元阵列:
就像魔术一样:)
更新:
坏消息,上述黑客在R2015b中不再起作用!没关系,它是未记录的功能,我们不能依赖它作为受支持的功能:)
对于那些想知道在哪里可以找到此类内容的人,请查看文件夹
fullfile(matlabroot,'bin','registry')< /代码>。那里有一堆 XML 文件,列出了各种好东西。请注意,直接调用其中一些函数很容易使您的 MATLAB 会话崩溃。
How do you feel about using undocumented features:
or for cell arrays:
Just like magic :)
UPDATE:
Bad news, the above hack doesn't work anymore in R2015b! That's fine, it was undocumented functionality and we cannot rely on it as a supported feature :)
For those wondering where to find this type of thing, look in the folder
fullfile(matlabroot,'bin','registry')
. There's a bunch of XML files there that list all kinds of goodies. Be warned that calling some of these functions directly can easily crash your MATLAB session.至少在 MATLAB 2013a 中,您可以使用
getfield
就像:获取 (1,2) 处的元素
At least in MATLAB 2013a you can use
getfield
like:to get the element at (1,2)
不幸的是,matlab 不支持像
magic(5)(3,3)
这样的语法。您需要使用临时中间变量。您可以在使用后释放内存,例如unfortunately syntax like
magic(5)(3,3)
is not supported by matlab. you need to use temporary intermediate variables. you can free up the memory after use, e.g.请注意,如果将运行时间与标准方式(分配结果然后访问条目)进行比较,它们是完全相同的。
在我看来,底线是:MATLAB 没有指针,你必须忍受它。
Note that if you compare running times with the standard way (asign the result and then access entries), they are exactly the same.
To my opinion, the bottom line is : MATLAB does not have pointers, you have to live with it.
可能会更简单:
如果你创建一个新函数,然后使用它,
It could be more simple if you make a new function:
and then use it:
您的初始符号是执行此操作的最简洁方法:
如果您在循环中执行此操作,则每次都可以重新分配 M 并忽略clear语句。
Your initial notation is the most concise way to do this:
If you are doing this in a loop you can just reassign M every time and ignore the clear statement as well.
为了补充 Amro 的答案,您可以使用
feval
而不是builtin
。实际上,没有什么区别,除非您尝试重载运算符函数:有趣的是,
feval
似乎比builtin
快一点点(约 3.5%),至少在 Matlab 2013b 中是这样,考虑到feval
需要检查函数是否重载,与builtin
不同:To complement Amro's answer, you can use
feval
instead ofbuiltin
. There is no difference, really, unless you try to overload the operator function:What's interesting is that
feval
seems to be just a tiny bit quicker thanbuiltin
(by ~3.5%), at least in Matlab 2013b, which is weird given thatfeval
needs to check if the function is overloaded, unlikebuiltin
: