Please check this query. Thank you!

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

1 view (last 30 days)

Show older comments

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;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

##### 12 Comments

Bruno Luong
on 14 Sep 2022

### Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!