How can I index a MATLAB array returned by a function without first assigning it to a local variable?

前端 未结 9 1856
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:02

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

9条回答
  •  深忆病人
    2020-11-21 05:59

    Note that if you compare running times with the standard way (asign the result and then access entries), they are exactly the same.

    subs=@(M,i,j) M(i,j);
    >> for nit=1:10;tic;subs(magic(100),1:10,1:10);tlap(nit)=toc;end;mean(tlap)
    
    ans =
    
    0.0103
    
    >> for nit=1:10,tic;M=magic(100); M(1:10,1:10);tlap(nit)=toc;end;mean(tlap)
    
    ans =
    
    0.0101
    

    To my opinion, the bottom line is : MATLAB does not have pointers, you have to live with it.

提交回复
热议问题