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]
  1 件のコメント
AbuYusuf
AbuYusuf 2017 年 6 月 7 日
Perfect! Thanks Stephen

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by