MatLab中for循环内矩阵的级联

MatLab中for循环内矩阵的级联,matlab,loops,matrix,populate,Matlab,Loops,Matrix,Populate,在MatLab中,我有一个矩阵SimC,它的维数为22 x 4。我使用for循环重新生成这个矩阵10次 我想以一个矩阵U结束,它在第1到22行中包含SimC(1),在第23到45行中包含SimC(2),依此类推。因此,U的最终尺寸应为220 x 4 谢谢 编辑: 不幸的是,上述方法不起作用,因为U=vertcat(SimC)只返回SimC,而不是串联。vertcat是一个不错的选择,但它会导致矩阵不断增长。这在大型程序上不是一个好的做法,因为它确实会减慢速度。不过,在您的问题中,您没有循环太多次

在MatLab中,我有一个矩阵SimC,它的维数为22 x 4。我使用
for
循环重新生成这个矩阵10次

我想以一个矩阵
U
结束,它在第1到22行中包含
SimC(1)
,在第23到45行中包含
SimC(2)
,依此类推。因此,U的最终尺寸应为220 x 4

谢谢

编辑:


不幸的是,上述方法不起作用,因为
U=vertcat(SimC)
只返回
SimC
,而不是串联。

vertcat
是一个不错的选择,但它会导致矩阵不断增长。这在大型程序上不是一个好的做法,因为它确实会减慢速度。不过,在您的问题中,您没有循环太多次,因此
vertcat
是可以的

要使用
vertcat
,您不会预先分配
U
矩阵的完整最终大小…只需创建一个空的
U
。然后,在调用
vertcat
时,需要为其提供两个要连接的矩阵:

nTrials = 10;
n = 22;
U = []      %create an empty output matrix
for i = 1 : nTrials
    SimC = SomeSimulation();    %This generates an nx4 matrix
    U = vertcat(U,SimC);  %concatenate the two matrices 
end  
由于您已经知道最终大小,因此更好的方法是预先分配您的全部
U
(如您所做),然后通过计算正确的索引将您的值放入
U
。大概是这样的:

nTrials = 10;
n = 22;
U = U = zeros(nTrials * n , 4);      %create a full output matrix
for i = 1 : nTrials
    SimC = SomeSimulation();    %This generates an nx4 matrix
    indices = (i-1)*n+[1:n];  %here are the rows where you want to put the latest output
    U(indices,:)=SimC;  %copies SimC into the correct rows of U 
end 

看看vertcat吧?谢谢,vertcat看起来很有前途。但是,我无法使其在代码内工作:
nTrials = 10;
n = 22;
U = U = zeros(nTrials * n , 4);      %create a full output matrix
for i = 1 : nTrials
    SimC = SomeSimulation();    %This generates an nx4 matrix
    indices = (i-1)*n+[1:n];  %here are the rows where you want to put the latest output
    U(indices,:)=SimC;  %copies SimC into the correct rows of U 
end