Hello,
I have a two cell array which are 3x1 and 2x1. I want to merge them and want to see as 5x1. How can I do it? When I use horzcat, It says dimensions are not consistent. Thank you.
{'01'} {'04'} {'01'}
{'02'} {'05'} ----> {'02'}
{'03'} {'03'}
{'04'}
{'05'}

 채택된 답변

Rik
Rik 2022년 6월 3일

0 개 추천

You need to use vertcat instead, or use the semicolon.
a={'01';'02';'03'}
a = 3×1 cell array
{'01'} {'02'} {'03'}
b={'04';'05'}
b = 2×1 cell array
{'04'} {'05'}
c=[a;b]
c = 5×1 cell array
{'01'} {'02'} {'03'} {'04'} {'05'}
vertcat(a,b)
ans = 5×1 cell array
{'01'} {'02'} {'03'} {'04'} {'05'}

추가 답변 (1개)

VINAYAK LUHA
VINAYAK LUHA 2022년 6월 3일
편집: VINAYAK LUHA 2022년 6월 3일

0 개 추천

Hi,
The reason horzcat doesn't work here is because the number of rows in both arrays are different , so you can't place them side by side , like
1 4
2 5
3
additionally , you won't get 5 x1 cell array you are looking for ,hence concat vertically using vertcat(X,Y) or like [X;Y] . Hope this solves your problem
X={'01';'02';'03'}
X = 3×1 cell array
{'01'} {'02'} {'03'}
Y={'04';'05'}
Y = 2×1 cell array
{'04'} {'05'}
Z=[X;Y]
Z = 5×1 cell array
{'01'} {'02'} {'03'} {'04'} {'05'}

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 6월 3일

편집:

2022년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by