How to avoid nested for loop to make the code faster?
Ältere Kommentare anzeigen
Hi everyone!
Do you have some suggestions to improve the following code?
As it is now, it is very slow…
B_2 = zeros(d,d);
for m = 1:M
for n = m+1 : 2*N
B_2 = B_2 + 4*N^(-1)* A(n) * B(n-m);
end
end
where A(n) and B(n) are 2x2 real, symmetric and full rank, matrices for all possible value of n.
Thanks in advance!
6 Kommentare
madhan ravi
am 14 Mär. 2019
Bearbeitet: madhan ravi
am 14 Mär. 2019
Without knowing the values of variables... M , N?
Stef
am 14 Mär. 2019
I doubt this will gain much speed, but since N appears to be constant you should pull
4*N^(-1)
out of the loops and calculate it once before the loops because it shouldn't change.
Also if A and B are 2x2 matrices how does B(n-m) work? There are only 4 valid indices for a 2x2 matrix so your loop cannot be very long if it only produces values of n and n-m between 1 and 4?
Stephen23
am 14 Mär. 2019
@Stef: please tell us the values of M, N, and d.
Stef
am 14 Mär. 2019
Stef
am 14 Mär. 2019
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!