How to make 2 for loops code to run faster?

1 Ansicht (letzte 30 Tage)
Amjad Iqbal
Amjad Iqbal am 13 Sep. 2022
Beantwortet: Pooja Kumari am 13 Sep. 2023
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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  19 Kommentare
Pooja Kumari
Pooja Kumari am 8 Sep. 2023
As you have told earlier that "dR is compensating the distance in the wavefronts using R0 and angle information." So why are you using
for ii = 1:2:length(angle)
ii
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
In the above provided for loop variable ii is taking values 1,3,5,7,9,.... so on. But you wanted to compenstate distance for all values of R0 and all angle informations.
Amjad Iqbal
Amjad Iqbal am 8 Sep. 2023
loop takes a lot of time and I used step 2 to reduce computational time.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Pooja Kumari
Pooja Kumari am 13 Sep. 2023
Dear Amjad,
I understand that you are facing issues with two for loops and wants to run the code faster. There are a few errors in your code which are as follows:
  1. You were using angle = size(data,1), it’s all the angle points stored in data of size(83348,235) in which 83348 are the angles presented in data and 235 are the datapoints. But you have used “size” function which is wrong in your code as it is providing only one value for angle instead of all the angles present in first column of data.
  2. As your loop is taking a lot of time, so you have used a step size of 2 to reduce computation time which is also wrong as it will not cover all the points of your angle presented in data(:,1).
  3. Another troubleshooting error that you are facing is:
% x_vect = (-5:0.02:5);
% y_vect = (-5:0.02:5);
x_vect = linspace(-5,5,size(data,1));
You can use “linspace” function for generating linearly spaced vectors. “linspace” gives direct control over the number of points you want to create, and, in this case, it will be equal to the size of angles present in data.
To run the code faster in MATLAB, you can use the following optimizing techniques:
  1. Avoid Unnecessary Calculations: Analyse your code to identify and remove any unnecessary or redundant calculations to reduce the computational load.
  2. Preallocate arrays: Preallocate arrays before using them in for loops. This means allocating memory for the arrays to their final size before the loops start executing.
  3. Vectorization: Replace inner loops with vectorized operations in MATLAB for faster execution as it is optimized for vector and matrix operations. Vectorization can be used to run code much faster than the corresponding code containing loops.
Here is the code for your reference to show the difference of time taken by vectorization and for loop to solve the example:
% Generate two random arrays
array1 = rand(1, 100000);
array2 = rand(1, 100000);
% Method 1: Using for loops
tic;
sum1 = 0;
sum3 = 0;
for i = 1:length(array1)
sum1 = sum1 + array1(i)^2 + array2(i)^2;
for j = 1: length(array2)
sum3 = sum1 + array1(j)^2;
end
end
toc
Elapsed time is 17.322632 seconds.
% Method 2: Vectorization
tic;
sum2 = sum(array1.^2 + array2.^2);
sum4 = sum(sum2 + array2.^2);
toc;
Elapsed time is 0.004939 seconds.
You can refer to the below documentation for more information:
I hope this helps!

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by