How to assign numbers to letters of the alphabet. Ex. A/a=1 B/b=2

8 ビュー (過去 30 日間)
Orion Sun
Orion Sun 2020 年 1 月 18 日
編集済み: Vladimir Sovkov 2020 年 1 月 18 日
As the title stated. I need to write a function where the input of a certain letter will give me the output of its corresponding number. (based off ASCII table)
Ex. someFunction (A)
ans = 1
someFunction (f)
ans = 6
Thanks for the help in advanced!

回答 (2 件)

Vladimir Sovkov
Vladimir Sovkov 2020 年 1 月 18 日
編集済み: Vladimir Sovkov 2020 年 1 月 18 日
The ASCII codes can be found with the function double
double('A');
65
If you want to get its ordinal number in the alphabet, use something like
k=double('A');
if k>64 && k<91
k=k-64;
elseif k>96 && k<123
k=k-96;
else
disp('not a letter')
end

Walter Roberson
Walter Roberson 2020 年 1 月 18 日

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by