Info
This question is closed. Reopen it to edit or answer.
is there a way to call vectors one by one from matrix other than "for" loop?
1 view (last 30 days)
Show older comments
I have a matrix [a1,a2,a3,a4; b,b,b,b; c,c,c,c], now I want to call each column for a pre-defined function f(a,b,c). There result will be 4x1 vector. I know how to do this with "for" loop, just wondering if there is a way to vectorize the calculation so that don't need to use the loop.
0 Comments
Answers (1)
Matt J
on 29 Mar 2015
Edited: Matt J
on 29 Mar 2015
Not for a general function f(). There are ways to hide the for-loop using arrayfun(), but that's not genuine vectorization.
Depending on the form of f(), however, you may be able to pass the entire matrix to f() and have it return a 4xN result whose columns are computed in a vectorized way. We would have to see the definition of f() to say more.
3 Comments
Matt J
on 29 Mar 2015
Edited: Matt J
on 30 Mar 2015
In any case, you can certainly do more vectorization within the for-loop, so that matrix-valued y can be handled
[m,n]=size(y);
h=zeros(m,n);
h(1,:)=omega;
for t=1:m %loop over rows of y
last=h(t-1,:);
h(t,:)=omega + alpha * last.*((y(t-1,:)-r)./sqrt(last)-lambda-gamma).^2 ...
+ beta * last;
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!