필터 지우기
필터 지우기

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

조회 수: 2 (최근 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?

채택된 답변

Aquatris
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by