我如何处理“使用vertcat的错误连接矩阵的尺寸在Matlab中不一致”?

时间:2017-06-22 13:29:19

标签: matlab matrix concatenation

所以我想连接一个m x n矩阵来获得1 x mn矩阵。我想连接的矩阵是从while循环生成的。虽然列数始终为3,但我无法确定每次迭代会有多少行。此外,每次迭代的行大小可能并不总是相同。

代码在行大小都等于6的情况下运行,但如果它们不相等,我会收到错误:

  

使用vertcat时出错连接的矩阵尺寸不一致。

部分代码如下:

A = [];
B = [];
searchArea = 2;

for ii = 1: numel(velocity)
    Do ....
    while area(ii,:) < searchArea
        Do ....
        % COLLATE vectors for A
        A = [A; [Ax(ii), Ay(ii), Az(ii)]];
        Do ...
    end
    %# Copy the A into new variable (B) and Reshape into row vector so as to associate each row to its corresponding velocity
    B = [B; reshape(A.',1,[])];
    A = [];
end

有人可以告诉我这里我做错了什么。如果有需要,我会进一步澄清。谢谢你们!

1 个答案:

答案 0 :(得分:0)

如果B最终成为行向量,那么您需要更改此内容:

B = [B; reshape(A.',1,[])];  % Does vertical concatenation

到此:

B = [B reshape(A.',1,[])];  % Does horizontal concatenation (note there's no semicolon)

以便从重塑A获得的每个行向量获取added to the end of the row而不是新行(as the semicolon indicates)。