vectorization of for loop
이전 댓글 표시
Hi,
I made a 2d matrix with two for loops:
for k = 1:32
for l = 1:32
P_new(l,k) = P_old(l) + (LODF(l,k) * P_old(k));
end
end
P_old is here a 32 x 1 matrix and LODF is a 32 x 32 matrix which is already computed. How can I vectorize this code to avoid the for loops? Thanks in advance.
채택된 답변
추가 답변 (2개)
Jos (10584)
2013년 12월 10일
for loops are pretty fast when you use pre-allocation
P_new = zeros(32,32) ;
for k = 1:32
for l = 1:32
P_new(l,k) = P_old(l) + (LODF(l,k) * P_old(k));
end
end
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!