[英]How do i handle “Error using vertcat Dimensions of matrices being concatenated are not consistent” in Matlab?
So I want to concatenate an mxn
matrix to obtain a 1 x mn
matrix. 所以我想连接
mxn
矩阵以获得1 x mn
矩阵。 The matrix I want to concatenate are generated from a while loop. 我想连接的矩阵是从while循环生成的。 Although the number of columns will always be
3
, I however cannot tell how many rows there will be for each iteration. 虽然列数总是
3
,但我无法确定每次迭代会有多少行。 Also, the row sizes for each iteration may not always be the same. 此外,每次迭代的行大小可能并不总是相同。
The code runs in cases where the row sizes were all equal to 6
, but in cases where they aren't equal I get an error: 代码在行大小都等于
6
情况下运行,但是在它们不相等的情况下我得到一个错误:
Error using vertcat Dimensions of matrices being concatenated are not consistent.
使用vertcat时出错连接矩阵的维数不一致。
parts of the code are as follows: 部分代码如下:
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
Could someone please advice me on what I am doing wrong here. 有人可以告诉我这里我做错了什么。 I would clarify further if there be need.
如果有需要,我会进一步澄清。 Thanks guys!
多谢你们!
If it's your intent that B
ends up being a row vector, then you need to change this: 如果你的意图是
B
最终成为行向量,那么你需要改变这个:
B = [B; reshape(A.',1,[])]; % Does vertical concatenation
to this: 对此:
B = [B reshape(A.',1,[])]; % Does horizontal concatenation (note there's no semicolon)
so that each row vector gotten from reshaping A
gets added to the end of the row instead of as a new row ( as the semicolon indicates ). 这样从重新整形
A
得到的每一行向量都会被添加到行的末尾,而不是作为一个新行( 如分号所示 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.