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!