Reduce computational time for nested for loops

1 visualización (últimos 30 días)
EldaEbrithil
EldaEbrithil el 18 de Jun. de 2021
Comentada: EldaEbrithil el 28 de Jun. de 2021
Hi all
I wondered how could I reduce computational time for this for loop:
for i=1:length(Lungh)
for j=i:length(Largh)
for k=1:length(T)
for w=1:length(Delta)
for av=1:length(A_deg_prua_verticale)
for h=1:length(H)
for b=1:length(B)
for ti=1:length(t)
if abs(CB(i,j,k,w)>0)
if Largh(j)<Lungh(i)
if T(k)<Lungh(i)
L_cb(i)=Lungh(i);
Larg_cb(j)=Largh(j);
T_cb(k)=T(k);
Dead_rise(k,j)=atand(Largh(j)/(2* T_cb(k)));
Delta_cb(w)=Delta(w);
H_triangolo(k,av)=(T_cb(k))*tand(A_deg_prua_verticale(av));
Aw_d(i,k,av)=(L_cb(i)-H_triangolo(k,av))*Larg_cb(j)+(Larg_cb(j)* H_triangolo(k,av)/2);
Awc(i,k,av)=2*Aw_d(i,k,av);
Cw(i,j,k,av)= Aw_d(i,k,av)/(L_cb(i)*Larg_cb(j));
Zc(i,j,k,w,av)=T_cb(k)/(1+(CB(i,j,k,w)/Cw(i,j,k,av)));
Ix_demihull(i,j,k,w,av)=(W_tonn(w)/(2*gamma))*((Cw(i,j,k,av)^2)*...
(Larg_cb(j)^2))/(11.4*CB(i,j,k,w)*T_cb(k));
Ix_upper(j,h,ti,b)=(B(b)-2*Larg_cb(j))*(((t(ti)^3)/3)+t(ti)*H(h)^2-H(h)*t(ti)^2);
Ix(i,j,k,w,av,h,ti,b)=2*Ix_demihull(i,j,k,w,av)+Ix_upper(j,h,ti,b);
ZG(j,k,h,ti,b)=((Largh(j)*H(h)^2)-(Largh(j)*T_cb(k)^2)+(B(b)*(H(h)^2)/2)-B(b)*(H(h)^2+t(ti)^2-2*H(h)*t(ti))/2-(Largh(j)*H(h)^2)+...
Largh(j)*(H(h)^2+t(ti)^2-2*H(h)*t(ti))+4*tand(Dead_rise(k,j))*(T_cb(k)^3)/3)/...
((Largh(j)*T_c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,h,ti,b)=T_teta(i,j,k,w,av,h,ti,b);
T_p_finale(i,j,k,w,av,h,ti,b)=T_p(i,j,k,w,av,h,ti,b);
T_heaving_finale(i,k,w,av)=T_heaving(i,k,w,av);
end
end
end
end
end
end
end
end
end
end
end
end
end
where length(Lungh) = 6, length(Largh)=9, length(T)=5, length(Delta)=7, length(A_deg_prua_verticale)=9, length(H)=11, length(B)=3, length(t)=3
Thank you for the help
Regards!

Respuesta aceptada

Sharmin Kibria
Sharmin Kibria el 25 de Jun. de 2021
Hi,
You can definitely speed up the process by vectorization.
Replacing the loops with vectorized solution can speed up the computation.
Thanks
Sharmin

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by