avoid nested for loop in this code to reduce processing time
1 view (last 30 days)
Show older comments
Hello, I am trying to create a code to do some calculations that requires 6 nested FOR loops. this requires too much processing time. I seek your assistance on how to avoid these nested FOR loops to reduce the processing time. I tried to use PARFOR but it does not reduce calculation time, it actually increases it. I try to use vectorization but since I am having 6 FOR loops, this would require to create 6-D arrays for the subject vectors in order to perform the calculation as matrix operations which makes things much more complicated. Appreciate your kind assistance. I attach a copy of the code to the question for further details. thanks and regards, Hossam
sensor=[122 25;118 60]; %sensor positions, 1st row for sensor 1 (x,y), 2nd row for sensor 2 (x,y) and so on in mm
n=1000;
n_points_x=101;
n_points_y=91;
depth=20;
c=6320; %speed of sound in Aluminum
t=linspace(0,n*1e-6,50000);
amplitude_each_point=zeros(n_points_x,n_points_y,2);
amplitude_sensors_all_depth_all_XZ_planes=zeros(n_points_x,depth,n_points_y);
for yii=1:n_points_y
pointgrid_y=(yii-1)*83/(n_points_y-1); %exact Y dimension of each laser transmitter point in y direction in mm
for xii=1:n_points_x
pointgrid_x=(xii-1)*92/(n_points_x-1); %exact X dimension of each laser transmitter point in x direction in mm
for d=0:depth-1
for ch=1:2
for yi=1:n_points_y
dim_y=(yi-1)*83/(n_points_y-1);
for xi=1:n_points_x
dim_x=(xi-1)*92/(n_points_x-1);
time=1/c*(sqrt((dim_y-pointgrid_y)^2+(dim_x-pointgrid_x)^2+d^2)+sqrt((pointgrid_y-sensor(ch,2))^2+(pointgrid_x-sensor(ch,1))^2)+d^2)*1e-3;
time_index_all=find(t>= time);
time_index=time_index_all(1);
amplitude_each_point(xi,yi,ch)=w(xi,time_index,yi,ch); % w is a 4-D array that is imported from an excel file containing the data I am extracting
end
end
end amplitude_sensors_all_depth_all_XZ_planes(xii,d+1,yii)=sum(sum(sum(amplitude_each_point(:,:,:))));
end
end
end
0 Comments
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!