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!