How to change alphabet matrix to integer matrix?

matrix = ['AADBC';'CADBC';'BACAC']
I have the above 3x5 character matrix. I would like to convert it to a number matrix where A=1, B=2... etc.
I tried:
matrix = ['AADBC';'CADBC';'BACAC'];
zboom = zeros(size(matrix));
zboom(matrix=='A') = 1;
zboom(matrix=='B') = 2;
zboom(matrix=='C') = 3;
zboom(matrix=='D') = 4
But is there a more efficient way to do this? What if I want it to be able to go up to all 26 alphabets? Is there a formula for it? Thank you.

 Réponse acceptée

the cyclist
the cyclist le 30 Mar 2018
Modifié(e) : the cyclist le 30 Mar 2018
zboom = matrix - 'A' + 1;
or
zboom = matrix - '@';
which is even more obfuscated :-)

2 commentaires

Thank you! This works very well!
By the way, using the alphabets limits me to 26 variables - is there a way to set up a character matrix where I can have combinations of alphabets instead? Thank you again.
Check out the documentation for the char command. Quoting that documentation:
"The integers from 32 to 127 correspond to printable ASCII characters. However, the integers from 0 to 65535 also correspond to Unicode® characters. You can convert integers to their corresponding Unicode representations using the char function.
For example, the number 8451 corresponds to the symbol for degrees Celsius."

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by