필터 지우기
필터 지우기

how to add string matrix to numeric matrix?

조회 수: 31 (최근 30일)
eri
eri 2012년 10월 26일
example:
from
a=[rice;corn;wheat]
b=[3;4;3]
become
c=[rice 3;corn 4;wheat 3]

답변 (3개)

José-Luis
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
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?
Walter Roberson
Walter Roberson 2012년 10월 26일
[a, strcat({' '}, num2str(b))] %output will be cell 3x2 of strings

댓글을 달려면 로그인하십시오.


Walter Roberson
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
eri 2012년 10월 26일
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?
Walter Roberson
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
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)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by