Using horzcat for string and regular matrix?

조회 수: 3 (최근 30일)
AbuYusuf
AbuYusuf 2017년 6월 7일
댓글: AbuYusuf 2017년 6월 7일
I have two matrices:
A={'ai' 'ba';'cr' 'dk'}
B=[1;2]
how can I put them together as following: C= [1 'ai' 'ba';2 'cr' 'dk']
I know this can be done using horzcat but for regular matrix. Is there a way to do it with two different matrix types like my case?
  댓글 수: 1
Stephen23
Stephen23 2017년 6월 7일
편집: Stephen23 2017년 6월 7일
Your output example does not make much sense: MATLAB will implicitly convert the types, and because [] is a concatenation operator you will end up joining all of those strings parts into a single 2x6 matrix. This is unlikely to be what you intended. Perhaps you really want the output to be a simple 2x3 cell array:
C = {1,'ai','ba';2,'cr','dk'}

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

채택된 답변

Stephen23
Stephen23 2017년 6월 7일
>> A = {'ai','ba';'cr','dk'};
>> B = [1;2];
>> C = [num2cell(B),A]

추가 답변 (0개)

카테고리

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