Matlab中具有动态尺寸的矩阵

时间:2019-05-23 13:22:58

标签: matlab matrix while-loop

在do-while循环中直至结束,在每次迭代中,应将新列添加到预定义的矩阵中。我找不到如何在Matlab中定义这样的矩阵?有没有一种标准的方法来定义具有动态尺寸的矩阵?

注意:do-while循环由for循环模拟

A=zeros(100,?);
for i=1:inf
A(:,i)=some computation ;

*condition*
end

2 个答案:

答案 0 :(得分:2)

尽管下面的代码可以工作,但是您可以考虑使用@Sardar_Usama建议,使用单元格。但是我对那些不是很好...

A = zeros(100, 20); % initial number of columns is 20, but can grow larger
col = 1;
while(condition)
    A(:, col) = result of some computation;
    col = col + 1;
end;

答案 1 :(得分:1)

matlab动态处理矩阵大小。因此,您可以轻松使用添加新的列或行。但是,它会降低算法的性能。一些例子:

A= ones(10,4);
B = zeros(10,2);
C = rand(3,4);
A = [A B]; % adds two new columns consisting of B to the end of A
A = [C; A];% adds three new rows consisting of C to the beginning of A