How to speed up this code consisting of 6 nested FOR loops?
Ältere Kommentare anzeigen
Hi All,
I am trying to speed up the code below which consists of 6 nested FOR loops.
The purpose of this code is to fill a large, sparse matrix A with ones, and a large vector b with elements from a matrix dd of size TxTxN.
I have access to the Parallel Computing Toolbox, and ultimately this code will run on a cluster.
Any comments/suggestions would be greatly appreciated.
N= big number (e.g. 10000)
T=24;
A=sparse(N*T*(T+1)/2,(N-1)*T+T); %matrix to be filled
b=sparse(N*T*(T+1)/2,1); %vector to be filled
tempT = (T+1)*T/2; % constant used in the loop
for j=1:N
for t1=1:T
for t2=t1:T
for t3=1:t2-t1+1
rw = (j-1)*tempT + (t1-1)*(T-t1/2)+t2;
cl = (j-1)*T+t1-1+t3;
A(rw,cl) = 1;
for t4=t1:t2
for t5=t4:t2
b(rw) = b(rw) + dd(t4,t5,j); % dd is given, size(dd)=TxTxN
end
end
end
end
end
end
An earlier discussion on a simplified version of this with only 3 nested loops can be found here: http://www.mathworks.com/matlabcentral/answers/52005-how-to-transform-these-three-nested-for-loops-into-a-parfor-loop
8 Kommentare
Matt Kindig
am 29 Okt. 2012
Did you run the Profiler on this code to identify where the bottlenecks are occuring? You could probably vectorize some of the code, but that won't necessarily improve performance. Also, if you wrap this in a function, it will typically run faster than a script.
Matt J
am 29 Okt. 2012
What is np and how does it compare in size to N?
Tanguy
am 29 Okt. 2012
Matt J
am 29 Okt. 2012
What are the dimensions of dd? Is it TxTxN ?
Tanguy
am 29 Okt. 2012
Chris E.
am 29 Okt. 2012
What exactly is the function of this code? I mean what are the inputs and the generated outputs, this way it will be easier to distinguish what needs to be done. Thanks!
Tanguy
am 30 Okt. 2012
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!