MATLAB: Is there a better (faster) way to code this

fast coding

Hi,
i am doing some matrix manipulation on quite big arrays (up to 4000×4000 elements). I am trying to find the best way to code the following:
for k=1:N
for w=1:N
u=(k-(N/2)); v=(w-(N/2));
cube=(u^2+v^2);
S(k,w)=exp(i*pi*l*cube);
end
end
Do you have any suggestions?
Thanks a lot in advance!

Best Answer

I don’t know what ‘l’ is so I can’t run your code.
I would avoid the loop and use meshgrid:
[U,V] = meshgrid(-N/2:N/2);
cube=(U.^2+V.^2);
S = exp(1i*pi.*l.*cube);
Related Question