Concatenate two cell colums with different length, in Variables window it is possible while horzcat gives "not consistent" error.

조회 수: 10 (최근 30일)
I have two celll columns.
a={'11';'21';'31'};
b={'12';'22'};
if I issue the
c=[a,b];
command it gives the error
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
However, if I do a
c=a;
and then I go to the Variables window and open 'c' and 'b' variables and copy the first (and only) column of 'b', I can paste the content ANYWHERE in the 'c' correctly. All the other (not involved) cell values become empty. How to reproduce it programmatically???

채택된 답변

madhan ravi
madhan ravi 2020년 7월 9일
ab = {a, b};
[Val, which_one] = max([numel(a),numel(b)]);
c = cell(Val, 2);
c(:, 1) = a;
c(1 : numel(b), 2) = b
% or perhaps you want
c = cell(5);
c(1:numel(a), 1) = a;
c(end - numel(b) + 1 : end, end) = b

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 7월 2일
c = {a, b} %? if this doesn't do what you want , you need to illustrate with an expected result.
  댓글 수: 3
madhan ravi
madhan ravi 2020년 7월 2일
편집: madhan ravi 2020년 7월 2일
ab = {a, b};
[Val, which_one] = max([numel(a),numel(b)]);
c = cell(Val, 2);
c(:, 1) = a;
c(1 : numel(b), 2) = b
% or perhaps you want
c = cell(5);
c(1:numel(a), 1) = a;
c(end - numel(b) + 1 : end, end) = b
Csaba
Csaba 2020년 7월 9일
편집: Csaba 2020년 7월 9일
Thanks, although it is a workaround (probably not possible to do it more elegantly) I would accept your answer if you would put it as an answer.

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by