简体   繁体   中英

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. The matrix I want to concatenate are generated from a while loop. Although the number of columns will always be 3 , I however cannot tell how many rows there will be for each iteration. 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:

Error using vertcat Dimensions of matrices being concatenated are not consistent.

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; 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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM