concatenate values from matrix and cell array

i have a matrix and a cell array
mtric =
1 2 3
4 5 6
7 8 9
cell_array =
aa bb cc
dd ee ff
gg hh ii
i wanted to combine both these as
new_out =
1 - aa 2 - bb 3 - cc
4 - dd 5 - ee 6 - ff
7 - gg 8 - hh 9 - ii

 採用された回答

Stephen23
Stephen23 2019 年 4 月 3 日

1 投票

>> M = [1,2,3;4,5,6;7,8,9];
>> C = {'aa','bb','cc';'dd','ee','ff';'gg','hh','ii'};
>> F = @(n,c) sprintf('%d - %s',n,c{:});
>> Z = arrayfun(F,M,C,'uni',0)
Z =
'1 - aa' '2 - bb' '3 - cc'
'4 - dd' '5 - ee' '6 - ff'
'7 - gg' '8 - hh' '9 - ii'

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 4 月 3 日

1 投票

strcat(""+mtric,{'-'}, string(cell_array)) % string array
cellstr(strcat(""+mtric,{'-'}, string(cell_array))) % cell array

4 件のコメント

Elysi Cochin
Elysi Cochin 2019 年 4 月 3 日
what is that double qoutes " "
it shows error in matlab
madhan ravi
madhan ravi 2019 年 4 月 3 日
Which version of matlab are you using? If not use string() instead of "".
Elysi Cochin
Elysi Cochin 2019 年 4 月 3 日
編集済み: Elysi Cochin 2019 年 4 月 3 日
i'm using 2016a
string is also not recognized
says undefined function or variable
madhan ravi
madhan ravi 2019 年 4 月 3 日
strcat(sprintfc('%d',mtric),{'-'},cell_array)

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

リリース

R2016a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by