Saving matrices inside a loop for each iteration

1 visualización (últimos 30 días)
Jack Davies
Jack Davies el 21 de Ag. de 2019
Comentada: Jack Davies el 21 de Ag. de 2019
I have a loop of the form:
for i = 1: length(k)
for j = 1: length(A)
for m = 1: length(alpha)
if Top == 1
[M, N] = QG_Two_Layer_Matrix(Num, k(i), l, S, ...
beta, A(j), alpha(m), Top, U);
k_arr((i-1)*2*Num + 1 : i*2*Num, j, m) = k(i); % Array to store k values for each A and alpha
elseif Top == 2
[M, N] = QG_Two_Layer_Matrix(Num, k, l(i), S, ...
beta, A(j), alpha(m), Top, U);
k_arr((i-1)*2*Num + 1 : i*2*Num, j, m) = l(i);
end
[vec, lambda] = eig(M, N);
eig_func(i, j, m, :, :) = vec(:, :);
eig_freq((i - 1)*2*Num + 1 : i*2*Num, j, m) = diag(lambda);
max_growth(i, j, m) = max(imag(diag(lambda)));
end
end
end
The arrays eig_func and eig_freq are very large and so my code is very slow for Num > 64... How can one overcome this?
A is a 1 x 50 column vector, alpha is a 1 x 12 column vector and k = linspace(-Num/2, Num/2 - 1, Num).
  2 comentarios
Stephen23
Stephen23 el 21 de Ag. de 2019
"...my code is very slow for Num > 64... How can one overcome this?"
As Ted Shultz wrote, you should preallocate the output arrays:
Ted Shultz
Ted Shultz el 21 de Ag. de 2019
If you highlight your code and click on the code icon, it is easier to read.

Iniciar sesión para comentar.

Respuestas (1)

Ted Shultz
Ted Shultz el 21 de Ag. de 2019
You are not preallocating those variables. If you do that, you will get a significant increase in speed. In some situations, I have had reductions in run times of several orders of magnitude.
To preallocate, just make the same variable before the loop starts of the size you expect it to be at the end (so it is filling in values rather than growing the matrix each loop)
  3 comentarios
Ted Shultz
Ted Shultz el 21 de Ag. de 2019
Do you really want to create a 5 dimensional variable that large? You are working with a very large number of values. Typically this would imply to me that you are either trying to save more information than intended, or you are solving a problem in a non-traditional way. You may want to step back and look at how you are trying to solve this.
If you are solving this the correct way, you may be able to save memory by using different types of variables rather than making everything a double. Or maybe you could use a bigger computer. But again, the approach I would take is go back and look at reworking the problem.
Jack Davies
Jack Davies el 21 de Ag. de 2019
Thank you for your input.
I shall take your advice and see if I can reformulate my approach.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by