简体   繁体   中英

Save matrix in for loop matlab

I have the following code:

for c=1:10;
D = maximin(n2,n1,'euclidean');
M = min(D,[],2); ;
C=[D M]; 
[maxValue, rowIdx] = max(C(:,end),[],1); %max value and the row index
n1(end+1,:) = n2(rowIdx,:); %Copy selected row to bottom of n1
n2(rowIdx,:) = []; %Delete the row from n2 that has the maximin
c=c+1;
end

n1 is 50*80 and n2 is 100*80 At the end of first iteration n1=51*80 and n2=49*80 and so on. I need to see save the n1 at the end of each iteration, so that I can use n1(1) ... n1(10) for further calculation. Please help. I tried the following

B = cell(1, c); B(n) = n1(1, c+1); and
B{n} = n1; 

Didn't help. Any help is very much appreciated.

Thanks

You should preallocate B before the loop as follows:

B = cell(10, 1);

and in each iteration of the loop you store n1 in B like so:

B{c} = n1;

Then you can access n1 computed the any iteration using the same syntax. For example, matrix n1 calculated in the the third iteration is B{3} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM