How to encode alphabets using tables

Hi I want to encode the word "CONGRATULATIONS" using upper case letters.For this I have the table
A 0
B 1
C 2
D 3 ......and so on.
I dont know how to relate this type of mapping in matlab.
Alphabets={'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z';'null space'};
numeric_values=[0;1;2 ;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26];
T1=table(Alphabets,numeric_values);
M={'C';'O';'N';'G';'U';'R';'A';'T';'U';'L';'A';'T';'I';'O';'N';'S'};
T2=table(M);

 採用された回答

R.G.
R.G. 2019 年 9 月 1 日
編集済み: R.G. 2019 年 9 月 1 日

0 投票

Hello! Check following code. I used char(65:90) to create A-Z sequence, and Map container to map symbols into codes.
symbols = [num2cell(char(65:90)) 'space'];
codes = 0:26;
dictionary = containers.Map(symbols, codes);
word = num2cell('CONGRATULATIONS');
encoded_word = cell(size(word));
for i = 1:length(word)
encoded_word{i} = dictionary(word{i});
end
display(cell2mat(encoded_word));

8 件のコメント

sadiqa ilyas
sadiqa ilyas 2019 年 9 月 1 日
It works .Thanks a lot.
R.G.
R.G. 2019 年 9 月 1 日
The same, but using tables
symbols = [num2cell(char(65:90)) "space"]';
codes = (0:26)';
dictionary = table(symbols, codes);
word = num2cell('CONGRATULATIONS');
encoded_word = cell(size(word));
for i = 1:length(word)
encoded_word{i} = dictionary.codes(dictionary.symbols == word{i});
end
display(cell2mat(encoded_word));
sadiqa ilyas
sadiqa ilyas 2019 年 9 月 1 日
thanks again
Walter Roberson
Walter Roberson 2019 年 9 月 1 日
There is an approach using table objects and Rownames that does not require any loops or explicit comparisons.
sadiqa ilyas
sadiqa ilyas 2019 年 9 月 3 日
can u plz give more hints or share links about this topic
Walter Roberson
Walter Roberson 2019 年 9 月 3 日
Alphabets={'A';'B';'C';'D';'E';'F';'G';'H';'I';'J';'K';'L';'M';'N';'O';'P';'Q';'R';'S';'T';'U';'V';'W';'X';'Y';'Z';'null space'};
numeric_values=[0;1;2 ;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26];
T1=table(numeric_values, 'rownames', Alphabets);
M={'C';'O';'N';'G';'U';'R';'A';'T';'U';'L';'A';'T';'I';'O';'N';'S'};
T1{M,:}
sadiqa ilyas
sadiqa ilyas 2019 年 9 月 3 日
Thanks. Its really a smart way for mapping.
Stephen23
Stephen23 2023 年 9 月 14 日
symbols = [num2cell(char(65:90)) 'space'];
codes = 0:26;
dictionary = containers.Map(symbols, codes);
word = num2cell('CONGRATULATIONS');
encoded_word = cell(size(word));
for i = 1:length(word)
encoded_word{i} = dictionary(word{i});
end
display(cell2mat(encoded_word))
2 14 13 6 17 0 19 20 11 0 19 8 14 13 18
A = 'A':'Z';
V = 0:25;
T = array2table(V(:),'rownames',num2cell(A(:)));
M = num2cell('CONGURATULATIONS');
T{M,:}
ans = 16×1
2 14 13 6 20 17 0 19 20 11
The MATLAB approach:
'CONGRATULATIONS' - 'A'
ans = 1×15
2 14 13 6 17 0 19 20 11 0 19 8 14 13 18

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

タグ

質問済み:

2019 年 9 月 1 日

コメント済み:

2023 年 9 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by