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!