Fast array as argument passing in MATLAB
I have a matrix I
, and I want to accumulate in an array A
, for each value in I
, an interval accordingly to those values in I
lets call them i
and j
.
function acc(i,j)
global A
A(i:j) = A(i:j)+1;
end
However, passing and returning the arrays take too much time, because I execute the function many times, and it i开发者_Go百科s not as simple as that example.
Is there any way of speeding it up? How can I pass an return those values without global?
The link in the comments proposes using a nested function as a solution. If the function you're using has use in several different places you may not want to nest in each place. It that case, I'd try to make the function modify in place.
http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/
function A = acc(A,i,j)
A(i:j) = A(i:j)+1;
end
This should not need to make a copy provided you meet the requirements set forth in Loren's blog post.
精彩评论