I appreciate advice on how I might vectorize the following for loop (matlab):
summ=0;
for i=1:lasti
summ=summ+abs(newTS(m+i*k)-newTS(m+(i-1)*k));
end
By vectorize I mean use matrix operations instead of the for loop. I have heard that vectorization is generally more efficient and quicker than for loops.
EDIT: Actually what I would really like to vectorize is below. I include it in case some brave soul wants to give it a shot...
for j=1:length(kvec)
k=kvec(j);
for m=1:k
lasti=floor((N-m)/k);
Nfact=(N-1)/(lasti*k);
summ=0;
for i=1:lasti
summ=summ+abs(newTS(m+i*k)-newTS(m+(i-1)*k));
end
L(m,j)=(summ*Nfact)/k;
%Avg over m
AvgL(j)=mean(L(:,j));
end
end
newTSis a function or just a matrix that you are extracting elements out of. IfnewTSis a function, it'll have to be vectorized before this loop can be fully optimized. - user57368AvgL(j)=mean(L(:,j));should be outside themloop. Please edit it. My edit was rejected. - LWZ