为matlab保存循环中的矩阵

为matlab保存循环中的矩阵,matlab,for-loop,Matlab,For Loop,我有以下代码: 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 f

我有以下代码:

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为50*80,n2为100*80,n1=51*80,n2=49*80,依此类推。我需要在每次迭代结束时保存n1,这样我就可以使用n1(1)。。。n1(10)用于进一步计算。请帮忙。我尝试了以下方法

B = cell(1, c); B(n) = n1(1, c+1); and
B{n} = n1; 
没有帮助。非常感谢您的帮助


谢谢

您应该在循环之前预先分配
B
,如下所示:

B = cell(10, 1);
在循环的每次迭代中,您将
n1
存储在
B
中,如下所示:

B{c} = n1;

然后,您可以使用相同的语法访问
n1
计算的任何迭代。例如,在第三次迭代中计算的矩阵
n1
B{3}

您得到了什么错误消息?你把第二段代码放在循环的什么地方了?我把它放在“c=c+1”语句的正上方。当使用B{n}时,我没有得到一个错误,相反,我得到了一个1*10的矩阵,其中每个单元格由n个组成(n1..n10在10个单元格中)。它就像整个n1矩阵在一个B{n}单元中一样,这太完美了!非常抱歉,它没有让我投票:(我有另一个外部循环k=50100和150。我为k=50100.150分别生成10个值。是否可以表示B{c}{k}?