how to add string matrix to numeric matrix?
古いコメントを表示
example:
from
a=[rice;corn;wheat]
b=[3;4;3]
become
c=[rice 3;corn 4;wheat 3]
回答 (3 件)
José-Luis
2012 年 10 月 26 日
You could use cell arrays:
a={'rice';'corn';'wheat'};
b={3;4;3};
your_result = cellfun(@(a,b) [a ' ' num2str(b)],a,b,'uniformoutput',false);
4 件のコメント
eri
2012 年 10 月 26 日
Walter Roberson
2012 年 10 月 26 日
It creates a cell array of strings, converting the numbers to strings and putting them at the end of the original strings.
Another way to do the same thing is
your_result = strcat(a, {' '}, num2str(b));
Note that in order for this to work without modification, the two arrays must be column arrays.
Walter Roberson
2012 年 10 月 26 日
[a, strcat({' '}, num2str(b))] %output will be cell 3x2 of strings
Walter Roberson
2012 年 10 月 26 日
No, numeric arrays can never contain strings.
Cell arrays can contain both strings and numbers, in separate elements.
c = {'rice', 3; 'corn', 4; 'wheat', 3};
2 件のコメント
eri
2012 年 10 月 26 日
Walter Roberson
2012 年 10 月 26 日
編集済み: Walter Roberson
2012 年 10 月 26 日
c = [a, num2cell(b)]; %output will be cell 3x2 first col strings second col numeric
Azzi Abdelmalek
2012 年 10 月 26 日
a={'rice';'corn';'wheat'}
b=[3;4;3]
c=arrayfun(@(x) [a{x} ' ' num2str(b(x))],1:3,'un',0)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!