how to add string matrix to numeric matrix?

回答(3 个)

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 个评论

can you give me explanation about your answer? it work fine with the example matrix above, but not with matrix i currently working on
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.
eri
eri 2012-10-26
编辑:eri 2012-10-26
it did work, but the result is in one column, is it possible to make it two columns, for the example above i have two 3x1 matrix and i want the result to be 3x2 matrix?
[a, strcat({' '}, num2str(b))] %output will be cell 3x2 of strings

请先登录,再进行评论。

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 个评论

my question is that i already have a and b, and what should i do to get output like c by operating a and b?
c = [a, num2cell(b)]; %output will be cell 3x2 first col strings second col numeric

请先登录,再进行评论。

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 的更多信息

标签

提问:

eri
2012-10-26

Community Treasure Hunt

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

Start Hunting!

Translated by