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.

 Respuesta aceptada

the cyclist
the cyclist el 30 de Mzo. de 2018
Editada: the cyclist el 30 de Mzo. de 2018
zboom = matrix - 'A' + 1;
or
zboom = matrix - '@';
which is even more obfuscated :-)

2 comentarios

YouBetterWerk
YouBetterWerk el 30 de Mzo. de 2018
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.
the cyclist
the cyclist el 30 de Mzo. de 2018
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."

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 30 de Mzo. de 2018

Comentada:

el 30 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by