Azzera filtri
Azzera filtri

How I can speed up of the 8 nested FOR loops with high values in each loop ?

2 visualizzazioni (ultimi 30 giorni)
Hello everyone , I have a long code that using 8 nested FOR loops and 4 of these loops contain of 30 values while another 4 loops contain of 20 values as follows:
M1 = M2 = M3 = M4= 1:30 ;
M5 = M6 = M7 = M8= 1:20 ;
for n1=1:size(M1,2)
for n2=1:size(M2,2)
for n3=1:size(M3,2)
for n4=1:size(M4,2)
for n5=1:size(M5,2)
for n6=1:size(M6,2)
for n7=1:size(M7,2)
for n8=1:size(M8,2)
My code
When I run this code its take long time and then restart my computer without any output, while if I use less value for example 3 for all 8 loops doesn't take long time and give me true result. can you help me to speed up my code to solve this problem please?

Risposta accettata

Aquatris
Aquatris il 18 Lug 2018
Depending on the problem you are trying to solve, you can convert the nested for loops into matrix algebra instead.
The reason why it is fast when you use 3 for all 8 loops is, the number of times the for loops run is around 3^8. When you use 20, it is 20^8 or so, which is a significant increase in the number of computations, hence time.
Otherwise, you might wanna explore Parallel Computing Toolbox if you have access to it.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by