Replacing numbers with letters in a matrix
Show older comments
I have a matrix like Matr=[2 1 2 1 4 1 3;3 4 3 4 3 4 4;1 3 1 3 2 3 2;4 2 4 2 1 2 1]; and i want to change 1 to a, 2 to b,3 to c and 4 to d. Finally i want to get a matrix like b a b a d a c; c d c d c d d; a c a c b c b; d b d b a b a;
but i had issues with matrix type. What is the ideal way to do.
Thank you very much
Accepted Answer
More Answers (1)
Steven Lord
on 15 Dec 2016
If all the elements of your matrix are positive integer values, you can use indexing.
Matr=[2 1 2 1 4 1 3;3 4 3 4 3 4 4;1 3 1 3 2 3 2;4 2 4 2 1 2 1];
letters = 'abcd';
Y = letters(Matr)
If you want to create a cell array of words or a string array you can do that too.
C = {'alfa', 'bravo', 'charlie', 'delta'};
S = string(C);
Y2 = C(Matr)
Y3 = S(Matr)
Categories
Find more on Tables 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!