Replacing numbers with letters in a matrix

17 views (last 30 days)
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

Friedrich
Friedrich on 5 Aug 2011
Hi,
I would do it this way:
char(Matr+96)
  4 Comments
Al
Al on 5 Aug 2011
thank you very much. You've just saved my day!
summyia qamar
summyia qamar on 15 Dec 2016
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)

Steven Lord
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 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!