Speed up nested loops
Ältere Kommentare anzeigen
I am trying to speed these loops up but I couldn't. please help me.
this code to calculate the shortest distance between lines and the distance between the mid point of the line and the the point of minimum distance vector between them then compare it with 0, if it is less than zero it will beark the second loop and change the orientation of the new line and check it with ALL previous lines. I tried 2000 lines with 100 trails the PC takes more than 48 hrs.
n=2000;
maxtrial=100;
Rx=rand(n,1)*0.2;
Ry=rand(n,1)*0.2;
Rz=rand(n,1)*0.2;
MT=0;
k=1;
while k<=n
% Create the angels for fiber oreintation
%For Phi
Phi_min=-0.1745329; %Angle Phi minimum value
Phi_max=0.1745329; %Angle Phi maximum value
Phi=Phi_min+rand(1,1)*(Phi_max-Phi_min);
%For theta
Z = (-1) + (1-(-1)) * rand(1,1); % value of Z to use in angle theta which will be within +- 10 degree
theta = acos (Z);
d1= sin(theta)*sin(Phi);
d2= sin(theta)*cos(Phi);
d3=cos(theta);
%first point coordinats
x2= Rx(k)+(Lf*0.5*d1);
y2= Ry(k)+(Lf*0.5*d2);
z2= Rz(k)+(Lf*0.5*d3);
%second point coordinats
x3= Rx(k)-(Lf*0.5*d1);
y3= Ry(k)-(Lf*0.5*d2);
z3= Rz(k)-(Lf*0.5*d3);
P=[x3-x2 y3-y2 z3-z2]; %orientation vector
P_all(k,:)=P;
if k>=2 && k<=n
t=k-1;
for t=1:k-1
normal_vector=((cross(P_all(k-t,:),P_all(k,:))/(norm(cross(P_all(k-t,:),P_all(k,:)))))); % unit vector normal to both lines
min_distance= norm(dot((R_all(k-t,:)-R_all(k,:)),normal_vector))-Df; % the minimum distance between two lines
L_ij=(-(dot((R_all(k-t,:)-R_all(k,:)),P_all(k-t,:)))+(dot((R_all(k-t,:)-R_all(k,:)),P_all(k,:)))*dot(P_all(k-t,:),P_all(k,:)))/(1-(dot(P_all(k-t,:),P_all(k,:)))^2); % distance between the center of line and the point that minimum distance occure at
if min_distance<0 && L_ij<=Lf/2
k=k-1;
MT=MT+1;
break
else
MT=0;
end
end
end
if MT==maxtrial
x2=0;
x3=0;
y2=0;
y3=0;
z2=0;
z3=0;
break
end
G1(k,:)= [z2 x2 y2]; %first points coordinates matrix
G2(k,:)= [z3 x3 y3]; %second points coordinates matrix
k=k+1;
end
4 Kommentare
Adam
am 15 Nov. 2019
Have you run the profiler
doc profile
to understand which lines are taking the most time? It's not perfect, given its disabling of certain run-time optimisations, but it usually gives a very good guide for where to focus speedup work.
Steven Lord
am 15 Nov. 2019
When you profile your program, I recommend doing so for a smaller number of lines and fewer trials. Doing so will not only let you see the performance bottlenecks sooner but will also let you see if there are any places your code assumes the larger number of lines or trials. If you decrease the number of lines to say 200 and receive an error about a line of code trying to access element 201 of an array, you should probably examine that line of code (and the code that creates or manipulates the variables used by that line) more closely.
You see warnings in the editor already, which explain the disadvantages of iteratively growing arrays. Use a proper pre-allocation in any way.
A simple code will not run faster, but it is easier to read: Compare
Z = (-1) + (1-(-1)) * rand(1,1)
with
Z = 2 * rand - 1;
I cannot run your code, because Lf, Df and R_all are undefined. It looks strange, that MT is not reset and comparing MT==maxtrial can match 1 time only.
Please post a running code.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Construct and Work with Object Arrays finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!