Convert an array of letters into numbers

I would like to convert an array of letters into numbers. Based on this code:
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = [3,6,12,1,1,3]
letters = Alphabet(numbers)
I would like to make the reverse of this. I mean I want to find eg 'HELLO' to which numbers correspont based on the above code.

Antworten (2)

Walter Roberson
Walter Roberson am 30 Jan. 2021
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
[~, HelloNumbers] = ismember('HELLO', Alphabet)
HelloNumbers = 1×5
8 5 12 12 15
Map(Alphabet) = 1:length(Alphabet);
Map('HELLO')
ans = 1×5
8 5 12 12 15

5 Kommentare

Ivan Mich
Ivan Mich am 30 Jan. 2021
Thank you. One more question. Is there a way to "include" to Alphabet accents to letters (I mean à for example). I mean to regognize these accents and to give them one specific number?
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÈÌÒÙÁÉÍÓÚÄËÏÖŸ';
Map(Alphabet) = 1:length(Alphabet);
Map('HÈLLÖ')
ans = 1×5
8 28 12 12 40
Ivan Mich
Ivan Mich am 31 Jan. 2021
Is there a way to set A equal to Á and À? I mean in order to not take into account accents?
Stephen23
Stephen23 am 31 Jan. 2021
"Is there a way to set A equal to Á and À? I mean in order to not take into account accents?"
Yes, two approaches were given to you in answers to your earlier question (which you then deleted):
Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÈÌÒÙÁÉÍÓÚÄËÏÖŸ';
Map(Alphabet(1:26)) = 1:26;
Map(Alphabet(27:end)) = Map('AEIOUAEIOUAEIOY');
Map('HÈLLÖ')
ans = 1×5
8 5 12 12 15

Melden Sie sich an, um zu kommentieren.

Ive J
Ive J am 30 Jan. 2021
Bearbeitet: Ive J am 30 Jan. 2021

0 Stimmen

One way would be Map object:
alph = 'A':'Z';
num = 1:numel(alph); % or whatever
M = containers.Map(string(alph'), num);
term = 'HELLO';
arrayfun(@(x) M(x), string(term')).'
ans =
8 5 12 12 15

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 30 Jan. 2021

Kommentiert:

am 31 Jan. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by