make replacements in an array
Show older comments
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];
Accepted Answer
More Answers (2)
Jan
on 10 Jun 2011
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
on 10 Jun 2011
0 votes
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 Comments
Michael
on 10 Jun 2011
Matt Tearle
on 10 Jun 2011
A = randi(3,1,10)
B = nominal(A,{'one','two','three'},1:3)
(if you have Stats TB)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!