Array dimensions must agree vertcat

조회 수: 5 (최근 30일)
Will Kimmerer
Will Kimmerer 2015년 10월 29일
댓글: Chad Greene 2015년 10월 29일
I've been trying to concatenate two cell arrays (both of which are 1x157) and I can use vertcat to do so in using the input/command line but I get an "Dimensions of matrices being concatenated are not consistent.:" error when I do so in a function. Is there anything I can do or will I have to perform the concatenation manually every time I need to?

답변 (1개)

Chad Greene
Chad Greene 2015년 10월 29일
My guess is both arrays are not 1x157. Is one of them 157x1? Because this works fine:
x = rand(1,157);
y = rand(1,157);
z = vertcat(x,y);
However,
x = rand(1,157);
y = rand(157,1);
z = vertcat(x,y);
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
  댓글 수: 2
Will Kimmerer
Will Kimmerer 2015년 10월 29일
편집: Will Kimmerer 2015년 10월 29일
When I output the two cell arrays I'm trying to concatenate as workspace variables they display as 1x157 cells. I'll check again though. Edit: They're the same size but it's still not working. One of them is an array of constants so I just ended up copying the data directly into the function and made a new variable for it.
Chad Greene
Chad Greene 2015년 10월 29일
Are you sure the constants are cells? Because
x = rand(1,157);
y = cell([1 157]);
z = vertcat(x,y);
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
But if you convert x to a cell array it works:
x = rand(1,157);
y = cell([1 157]);
z = vertcat(num2cell(x),y);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by