How to replace letters with numbers in matrix
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
DGM
am 24 Feb. 2022
Bearbeitet: DGM
am 24 Feb. 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 Kommentare
DGM
am 24 Feb. 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,', ','')
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!