空的()在Matlab矩阵上有什么作用? [英] What does the empty `()` do on a Matlab matrix?
问题描述
在Matlab
中,如果M
是矩阵,表达式M()
到底能做什么?
In Matlab
, what exactly does the expression M()
do where M
is a matrix?
>> M = magic(3);
>> M()
ans =
8 1 6
3 5 7
4 9 2
在所有情况下表达式isequaln(M, M())
是否都正确?是M()
只是M
的副本还是相同的表达式,或者在引用M()
的意义上除了引用M
以外还有其他含义吗?也许是在操作员超载的情况下?
Is the expression isequaln(M, M())
true under all circumstances? Is M()
simply a copy of M
, or an identical expression, or is there any context where referring to M()
means something else than referring to M
? Maybe in the case of operator overloading?
推荐答案
除了它会在某些功能(例如rand()
)和复活节彩蛋(例如imagesc()
和spy()
(这也可以在没有()
的情况下使用,只要访问数组的维数小于5(似乎是@Rody Oldenhuis),这似乎是访问整个数组的一种更有效的方法.例如:
Besides the fact the it would give the default operation on some function, such as rand()
, and easter eggs such as imagesc()
and spy()
(this will work also without the ()
) , it seems to be a more efficient way to access whole arrays as long as their dimensionality is below 5 (as @Rody Oldenhuis spotted) . For example:
a=rand(2^12);
tic
for j=1:1e5
a ;
end
toc
tic
for j=1:1e5
a(:) ;
end
toc
tic
for j=1:1e5
a() ;
end
toc
产量:
Elapsed time is 0.047250 seconds.
Elapsed time is 0.022260 seconds.
Elapsed time is 0.011925 seconds.
但是,对于作业,a1=a
与a1=a()
之间几乎没有区别,后者的速度要慢1.5%...
However, for assignments there's very little difference between a1=a
vs a1=a()
, where the latter is slower by 1.5%...
也许该线程会回答您有关运算符重载的一些问题.
Perhaps this thread will answer some of your question regarding operator overloading.
这篇关于空的()在Matlab矩阵上有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!