Convert an array of letters into numbers

조회 수: 70 (최근 30일)
Ivan Mich
Ivan Mich 2021년 1월 30일
댓글: Walter Roberson 2021년 1월 31일
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.

답변 (2개)

Walter Roberson
Walter Roberson 2021년 1월 30일
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
Stephen23
Stephen23 2021년 1월 31일
"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):
Walter Roberson
Walter Roberson 2021년 1월 31일
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

댓글을 달려면 로그인하십시오.


Ive J
Ive J 2021년 1월 30일
편집: Ive J 2021년 1월 30일
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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by