# How to make 2 for loops code to run faster?

1 view (last 30 days)
Amjad Iqbal on 13 Sep 2022
Commented: Bruno Luong on 14 Sep 2022
Dear MATLAB Experts;
I want to make this part of code to run faster it takes hours to process. there are two for loops which take a lot of time to compile the code.
I need your guidance to make this code run faster.
Thank you so much.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_vect = (-5:0.02:5);
y_vect = (-5:0.02:5);
z_vect = 0;
[x_mat,y_mat,z_mat] = meshgrid(x_vect,y_vect,z_vect);
img = zeros(size(x_mat));
tic;
for ii = 1:2:length(angle)
dR = sqrt((R0*cos(alpha*pi/180).*cos(angle(ii)*pi/180) + x_mat).^2 ...
+ (R0*cos(alpha*pi/180).*sin(angle(ii)*pi/180) + y_mat).^2 +...
(z_mat - R0*sin(alpha*pi/180)).^2) - R0;
for jj = 1:length(Fvec)
img = img + data(ii,jj).*exp(1j*4*pi*Fvec(jj)/c0*dR);
end
end
toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Bruno Luong on 14 Sep 2022
It is NOT 2D filter or convolution. Your "kernel" change form. It is NOT Fourier transform either. Tere is perhaps no fancy algorithms that can possiblyy reduces the flops of your calculation.

### Categories

Find more on Matrices and Arrays in Help Center and File Exchange

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!