MATLAB: Nested for loops newbie question

for loopsnested

Hi,
I know this might be nested for loops question 1000. I have read most of other posts, even the newbie questions seems little bit advanced. My code is simple, yet I don't know where to look at to improve its speed. This loops takes more than 170 sec for N=512 M=128.
Thanks
for n = 1:M
for m = 1:M
for k = 1:N
for l = 1:N
temp=temp+fun(k,l)*exp(-i*2*pi*( n*t(k)/T + m*t(l)/T));
end
end
c(n,m) = coef*temp;
temp=0;
end
end

Best Answer

So you have some nested loops and you want to make your code faster.
The first step is to read this page:
and in particular you need to understand when arrays need to be preallocated before loops:
and the importance of code vectorization:
In general there is no magic solution for speeding up some looped code: it depends on the operations being performed. In your case the operations are quite simple, and could be vectorized quite easily. Here is a vectorized version that runs in a quarter of the time (I did not test it on the full size matrices):
% Constants:
Mn = 128;
Nn = 51;
%Mn = 128;
%Nn = 512;
temp = 0;
fun = randi(9,Nn,Nn);
t = 1:Nn;
T = 10;
coef = 3;
old = NaN(Mn);
% Your code:
tic
for M1 = 1:Mn
for M2 = 1:Mn
for N1 = 1:Nn
for N2 = 1:Nn
temp=temp+fun(N1,N2)*exp(-1i*2*pi*( M1*t(N1)/T + M2*t(N2)/T));
end
end
old(M1,M2) = coef*temp;
temp=0;
end
end
toc
% Vectorized version:
tic
[N1m,~,M1m,~] = ndgrid(1:Nn,1,1:Mn,1);
[~,N2m,~,M2m] = ndgrid(1,1:Nn,1,1:Mn);
tmp = bsxfun(@plus,M1m.*t(N1m)/T,M2m.*t(N2m)/T);
tmp = bsxfun(@times,fun,exp(-1i*2*pi*tmp));
new = coef * permute(sum(sum(tmp,1),2),[3,4,1,2]);
toc
% Maximum difference in outputs:
max(max(abs(old-new)))