how to add string matrix to numeric matrix?
74 次查看(过去 30 天)
显示 更早的评论
example:
from
a=[rice;corn;wheat]
b=[3;4;3]
become
c=[rice 3;corn 4;wheat 3]
0 个评论
回答(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);
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 个评论
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!