필터 지우기
필터 지우기

make replacements in an array

조회 수: 1 (최근 30일)
Michael
Michael 2011년 6월 10일
i have an array of doubles, A.
A = [0; 0; 1; 1; 0; 1];
I wish to replace all the 1's with 'yes' and the 0's with 'no'. can this be done in order to produce an output B?
B = [no; no; yes; yes; no; yes];
  댓글 수: 1
Michael
Michael 2011년 6월 10일
ok, in understand the whole character thing, but just say i want to add a 3rd option to the mix..
A = [1; 2; 3]
how do i get:
B = ['one '; 'two '; 'three']
?
thanks again everyone

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

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 6월 10일
B must be a cell array.
A = [0; 0; 1; 1; 0; 1];
B = repmat({'no'},size(A));
B(logical(A)) = {'yes'}
Part 2
Numbs = {'one';'two';'three'}; %etc
A = [1;2;3];
B = Numbs(A)
  댓글 수: 1
Michael
Michael 2011년 6월 10일
i was making this way to complicated, thanks

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

추가 답변 (2개)

Jan
Jan 2011년 6월 10일
A = [0; 0; 1; 1; 0; 1];
C = {'no', 'yes'}
B = reshape(C(A + 1), [], 1);
But as Matt T. said: Now B is a CELL string, which is not exactly "[no; no; yes; yes; no; yes]", because this is not possible in Matlab.

Matt Tearle
Matt Tearle 2011년 6월 10일
You can't have a matrix of strings 'yes' and 'no', like you've shown. You can have logicals (true/false), but they will display like 1/0. You can make a cell array of strings. You can make a character array of strings by padding the 'no's with a space ('no '). Or, if you have Statistics Toolbox, you can make a nominal array.
What is your purpose in all of this?
  댓글 수: 2
Michael
Michael 2011년 6월 10일
basically for work i have to produce reports and there are different types of data that i look at and wanted to tag each type of data with strings. right now i just have numbers to tag them instead of character labels. this was just a simple example. there are actually 3 types of data that i tagged as 1, 2, and 3.
Matt Tearle
Matt Tearle 2011년 6월 10일
A = randi(3,1,10)
B = nominal(A,{'one','two','three'},1:3)
(if you have Stats TB)

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

카테고리

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