Effacer les filtres
Effacer les filtres

How I can speed up My code that contain of 8 FOR nested loops ?

1 vue (au cours des 30 derniers jours)
A
A le 7 Août 2018
Modifié(e) : Stephen23 le 12 Août 2018
Hi every one , I have a code that contain of 7 nested FOR loops, each loop from 1:30 . My idea of this code is calculate many of a special functions for each value of these loops and many of files need to calling, for example :
A1= 1:30
A2= 1:30
A3=1:30
A4=1:30
A5=1:30
A6=1:30
A7=1:30
for b1=1:length(A1)
for b2= 1: length(A2)
for b3= 1:length(A3)
for b4=1:length(A4)
for b5= 1: length (A5)
for b6=1:length(A6)
for b7=1:length(A7)
N(b1,b2,b3,b4,b5,b6,b7)=M(b1,b2,b3,b4,5b,b6,b7)*T(b1,b2,b3,b4,5b,b6,b7)
end
end
end
end
end
end
end
where M and T are have a unique value for each iteration , for example M(1,1,1,1,1,1,1)=5 , M(1,2,1,1,1,1,1)=8 and so on , when I run my code its tack long time and then restart my computer without any result can you help me to speed up this code ? thank you in advance .
  7 commentaires
Adam Danz
Adam Danz le 8 Août 2018
You haven't answered the question as to whether 'A' is a function or a 7D array. If it's an array and this example really represents what you want to do you might be able to avoid loops and replace them with a vector method which may (or may not) speed things up.
Nevertheless, given the little I know about your problem, it seems that another solution may be to process your data in chunks. For example, you could save data to a file every time A=3 loop is complete and then free up the memory in matlab so it's available for the next series of loops.
OCDER
OCDER le 8 Août 2018
Even without knowing the class of M and T, it takes a while. 30^7 is 21,870,000,000 iterations. That's ~45s to just go through the for loop.
tic
for j = 1:30^7
end
toc %45s
Any math inside is just going to make the things slower. You may need to use parfor to divide up the compute time, or vectorize a segment of the loop to prevent memory error.

Connectez-vous pour commenter.

Réponse acceptée

David Goodmanson
David Goodmanson le 10 Août 2018
Modifié(e) : David Goodmanson le 10 Août 2018
Hi A,
I am assuming that M,T and N are 7d arrays of the same size, n x n x n x n x n x n x n for some n. For n = 30 that's 30^7 = 2.1870e+10 elements per array. Not much to say about the memory requirements, they are what they are.
As far as speed, though, in place of all the for loops you can write
X = M(:).*T(:);
NN = reshape(X,n,n,n,n,n,n,n);
On my pc the for loops start to take significant time around n = 12. It's about 8 seconds for the for loop version, compared to .2 sec for the code just above, and the ratio will get more pronounced as n increases.
----------p.s. on for loops:
You have defined A1 = 1:n (1:30 in your case), so rather than write
for b1 = 1:length(A1) etc.
you can just write
for b1 = A1 etc.
Also it makes sense to preallocate N as
N = zeros(n,n,n,n,n,n,n)
  1 commentaire
A
A le 12 Août 2018
Modifié(e) : Stephen23 le 12 Août 2018
Thank you David , I already determinant N=zeros(1:length(A1),1:length(A2),1:length(A3),1:length(A4),1:length(A5),1:length(A6)) to speed my code but I did not notice any change in speed. And for b1 = A1 etc it's give me error because I need all element in my code as for I want N if A1=1,A2=1,A3=1,A4=1,A5=1 and A6=1 to compare with N if A1=2,A2=1,A3=1,A4=1,A5=1 and A6=1 and so on . I can't know the code problem because its take a long time without give me any error message in command window :(

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by