Saving matrices inside a loop for each iteration

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 Kommentare

Stephen23
Stephen23 am 21 Aug. 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 am 21 Aug. 2019
If you highlight your code and click on the code icon, it is easier to read.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ted Shultz
Ted Shultz am 21 Aug. 2019

0 Stimmen

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 Kommentare

Jack Davies
Jack Davies am 21 Aug. 2019
Thank you for the response! Despite preallocating variables, when I attempt to use Num = 128, I obtain the error:
Error using zeros
Requested 128x50x12x256x256 (37.5GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long
time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
The preallocated arrarys have the form:
k_arr = zeros(2*Num*length(k), length(A), length(alpha));
eig_func = zeros(length(k), length(A), length(alpha), 2*Num, 2*Num);
eig_freq = zeros(length(k), length(A), length(alpha));
max_growth = zeros(length(k), length(A), length(alpha)).
Again, thank you for your contribution to this post!
Ted Shultz
Ted Shultz am 21 Aug. 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 am 21 Aug. 2019
Thank you for your input.
I shall take your advice and see if I can reformulate my approach.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 21 Aug. 2019

Kommentiert:

am 21 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by