Replacing numbers with letters in a matrix
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (1)
Steven Lord
am 15 Dez. 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)
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!