How to replace letters with numbers in matrix
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/905175/image.png)
2 commentaires
Réponses (1)
DGM
le 24 Fév 2022
Modifié(e) : DGM
le 24 Fév 2022
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 commentaires
DGM
le 24 Fév 2022
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,', ','')
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!