我有一些算法,包括while
循环:
while (condition)
% do something and return a result array B
end
让我们说:
-Loop 1: B1=[1 2 3 4 5....9];
-Loop 2: B2=[10 11 12....15];
-Loop 3: B3=[16 17 18 19] ;
-Loop 4: B4=[20 21 22....30];
如何在循环结束时创建单元格A={B1,B2,B3,B4}
?
对于我的实际数据,while
循环可能循环100次或更多次,以上是简化。
答案 0 :(得分:1)
您可以使用end
关键字
% Initialise empty cell array
A = {};
while *condition*
% Create B using some calculation (different each loop)
B = [1 2 3];
% Other code ...
% Assign to array
A{end+1} = B;
end
答案 1 :(得分:-1)
您可以使用horzcat或[]加入阵列。然后初始化一个单元格并将它们保存到该单元格中。
%% loop
count = 0 ;
iwant = cell([],1) ;
while .....
count = count+1 ;
%% let be your arrays
B1=1:9 ;
B2=10:15 ;
B3=16:19 ;
B4=20:30 ;
%% join them into array
B = [B1 B2 B3 B4] ;
iwant{count} = B ;
end