Info

此问题已关闭。 请重新打开它进行编辑或回答。

is there a way to call vectors one by one from matrix other than "for" loop?

1 次查看(过去 30 天)
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.

回答(1 个)

Matt J
Matt J 2015-3-29
编辑:Matt J 2015-3-29
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 个评论
Matt J
Matt J 2015-3-29
编辑:Matt J 2015-3-29
That function does not look like it produces a 4x1 output. It looks like it returns a scalar. Where are a,b,c?
Matt J
Matt J 2015-3-29
编辑:Matt J 2015-3-30
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

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by