MATLAB: Vectorization of for loop

for loopnestedvectorize

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));
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.

Best Answer

  • P_new= bsxfun(@times, LODF, P_old.');
    P_new= bsxfun(@plus, P_new,P_old);