Avoid using for loops as it results in ram crash and slow down

My code is:
for i = 1:n
for ii = 1:i-1
for iii = 1:ii-1
for iv = 1:iii-1
A = data(l([i ii iii iv 27]), 1:3);
% C(i,ii) = norm(A)*norm(inv(A));
% C(i,ii,iii) = norm(A)*norm(inv(A));
C(i,ii,iii, iv, 1) = cond(A);
% tmp = svd(A, 0);
% C(i,ii,iii,iv) = 0;
% vif{i,ii,iii} = diag(inv(A'*A));
% vif1{i,ii,iii} = diag(inv(A*A'));
end
end
end
i
end
The loop test for only 4 different wavelengths and here I am fixing the the 5th by using 27 in "A = data(l([i ii iii iv 27]), 1:3)", I am interested in adding the fifth as a loop or maybe 6th as well but the problem my ram is crashing and slow down the code a lot.
Any suggestion on how to solve this problem?
Regards,

5 Comments

Be sure to pre-allocate memory for the variables (like C) that you fill inside of your loop.
Hi for i=1:n, mean (i=n:-1:1, might the correct) has to start with 1 it imply in for ii, has output of 1-1=0
Thanks for the pre-allocate memory, I have already done that I just did not include it in the code. the problem is that I am creating a matrix of size 200*200*200*200*200 which is huge. Therefore I was wondering if there is a better way to deal with this situation.
Thanks for the replay,
Only allocate 200*200*200*200*1 as the maximum you use in the final dimension is 1.
Yes as I stated I am only using 4, but I want to use more for more wavelengths. Any suggestion on how can I do that without crashing the RAM?

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 2 Dec 2015

Commented:

on 15 Dec 2015

Community Treasure Hunt

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

Start Hunting!