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.
0 件のコメント
回答 (1 件)
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
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!