How to replace letters with numbers in matrix
8 ビュー (過去 30 日間)
古いコメントを表示
Hi, so I would like to replace A with 4, B with 3, C with 2, D with 1, and F with 0 in this matrix, here is the code that I have:
m = [D, 2, F, 1, C, 2, A, 3, B, 2, B, 1, C, 2, A, 1, C, 2, F, 2, D, 2, B, 3, C, 3, A, 2, F, 1, A, 2, F, 1, C, 1, F, 2, A, 1, A, 1, A, 3, F, 1, D, 1, B, 2, B, 1, B, 2, B, 2, D, 3, D, 1, F, 1, F, 3, D, 2, C, 2, D, 3, A, 1, A, 2]
str2num(replace(text, ["A", "B", "C", "D", "F"], ["4", "3", "2", "1", "0"]))
Unfortunately this didn't work, what else should I do?
2 件のコメント
回答 (1 件)
DGM
2022 年 2 月 24 日
編集済み: DGM
2022 年 2 月 24 日
Here's one way:
m = 'D2F1C2A3B2B1C2A1C2F2D2B3C3A2F1A2F1C1F2A1A1A3F1D1B2B1B2B2D3D1F1F3D2C2D3A1A2';
m2 = replace(m,{'A','B','C','D','F'},{'4','3','2','1','0'})
m2 = str2num(m2.').'
Alternatively:
m = 'D2F1C2A3B2B1C2A1C2F2D2B3C3A2F1A2F1C1F2A1A1A3F1D1B2B1B2B2D3D1F1F3D2C2D3A1A2';
map = [1:9 4 3 2 1 0 0]; % note that E is missing
m2 = map(hex2dec(m.'))
2 件のコメント
DGM
2022 年 2 月 24 日
m = 'D, 2, F, 1, C, 2, A, 3, B, 2, B, 1, C, 2, A, 1, C, 2, F, 2, D, 2, B, 3, C, 3, A, 2, F, 1, A, 2, F, 1, C, 1, F, 2, A, 1, A, 1, A, 3, F, 1, D, 1, B, 2, B, 1, B, 2, B, 2, D, 3, D, 1, F, 1, F, 3, D, 2, C, 2, D, 3, A, 1, A, 2';
m = strrep(m,', ','')
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!