How I can speed up of the 8 nested FOR loops with high values in each loop ?
1 回表示 (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
Aquatris
2018 年 7 月 18 日
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.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!