Replacing numbers with letters in a matrix

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

Hi,
I would do it this way:
char(Matr+96)

4 Comments

thank you it works. but what if if i want to change them to arbitrary letters. 1 to z, 2 to g etc.
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];
res = char(zeros(size(Matr)));
res(Matr==1) = 'z';
res(Matr==2) = 'g';
res(Matr==3) = 'l';
res(Matr==4) = 'a';
thank you very much. You've just saved my day!
i'm doing this
m=[2 2.8 2.5 2 2.5 2.8 2.5 37243]; res=char(zeros(size(m))) res(m==2)='worker1' res(m==2.5)='worker2' res(m==2.8)='worker3' res(m==3)='worker4'
but the output message shows In an assignment A(:) = B, the number of elements in A and B must be the same.

Sign in to comment.

More Answers (1)

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

Asked:

Al
on 5 Aug 2011

Answered:

on 15 Dec 2016

Community Treasure Hunt

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

Start Hunting!