How to make 2 for loops code to run faster?
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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19 Comments
Amjad Iqbal
on 13 Sep 2022
Walter Roberson
on 13 Sep 2022
We need some additional information. In particular we need to know whether R0 and alpha and c0 are scalar.
Amjad Iqbal
on 13 Sep 2022
Edited: Amjad Iqbal
on 13 Sep 2022
Walter Roberson
on 13 Sep 2022
I am left uncertain about the size of alpha ?
Also, you say Nrange = size(data) but size() with a single parameter returns a vector, and you then use the vector in a colon operation. Perhaps Nrange = size(data,1) ?
Amjad Iqbal
on 14 Sep 2022
Edited: Walter Roberson
on 6 Sep 2023
Bruno Luong
on 14 Sep 2022
angle = size(data,1);
I guess you really want is to generate a (random) vector of size(data,1) NOT a scalar of value size(data,1)
Amjad Iqbal
on 14 Sep 2022
Edited: Amjad Iqbal
on 14 Sep 2022
Bruno Luong
on 14 Sep 2022
You don't understand: Angle is angle in degres NOT a size(data,1) as your code written. What you really mean is length(angle) is size(data,1), something like
angle = 2*pi*rand(1,size(data,1));
NOT obviously wrong statement
angle = size(data,1)
Bruno Luong
on 14 Sep 2022
Edited: Bruno Luong
on 7 Sep 2023
I'm affraid your code does a lot of calculations, very demanding. One can reduce a little bit by matrix multiplication but it is very very expansive.
Amjad Iqbal
on 14 Sep 2022
Amjad Iqbal
on 14 Sep 2022
Bruno Luong
on 14 Sep 2022
Edited: Bruno Luong
on 7 Sep 2023
It is NOT 2D filter or convolution. Your "kernel" change form. It is NOT Fourier transform either. There is perhaps no known fancy algorithms that can possibly reduce the flops of your calculation.
Manas
on 6 Sep 2023
What does variables R0 and dR do? Are they dependent on each other?
Pooja Kumari
on 6 Sep 2023
What is angle in your code?
Amjad Iqbal
on 7 Sep 2023
Amjad Iqbal
on 7 Sep 2023
its 2D data of size (83348, 235) which represents (angle, data_points)
So matrix has angles information on dimension-1 and data points on dimension-2."
No I signaled already the problem but you did seem to grab it. By this statement
% angle = size(data,1)
you set angle to a scalar of value = 83348, this is NOT what you describe and obviously not intended behaviour. Your "loop" will run only once with ii = 1.
Your demo code is faulty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = rand(83348, 235);
Nrange = size(data,2);
alpha = 3;
win_ham = repmat(hamming(size(data,2)).',size(data,1),1); %
data = data.*win_ham;
c0 = 3e8;
R0 = 135.16;
Fc = 9.6e9;
Bchirp = 0.5e9;
Fvec = Fc + (-0.5:1/Nrange:0.5-1/Nrange)*Bchirp;
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));
angle = size(data,1),
tic;
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
toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Pooja Kumari
on 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
on 8 Sep 2023
Answers (1)
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:
- 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.
- 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).
- 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:
- Avoid Unnecessary Calculations: Analyse your code to identify and remove any unnecessary or redundant calculations to reduce the computational load.
- 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.
- 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
% Method 2: Vectorization
tic;
sum2 = sum(array1.^2 + array2.^2);
sum4 = sum(sum2 + array2.^2);
toc;
You can refer to the below documentation for more information:
I hope this helps!
Categories
Find more on Contrast Adjustment in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!