Filter löschen
Filter löschen

How to replace letters with numbers in matrix

4 Ansichten (letzte 30 Tage)
Sherman Ng
Sherman Ng am 24 Feb. 2022
Kommentiert: DGM am 24 Feb. 2022
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
KSSV
KSSV am 24 Feb. 2022
How is your m? What class it is?
Sherman Ng
Sherman Ng am 24 Feb. 2022
m is supposed to be the matrix that I have.
Also I got this error:
Unrecognized function or variable 'D'.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

DGM
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 = '12012243323122412202123323420142012102414143011132313232131101031222134142'
m2 = str2num(m2.').'
m2 = 1×74
1 2 0 1 2 2 4 3 3 2 3 1 2 2 4 1 2 2 0 2 1 2 3 3 2 3 4 2 0 1
Alternatively:
m = 'D2F1C2A3B2B1C2A1C2F2D2B3C3A2F1A2F1C1F2A1A1A3F1D1B2B1B2B2D3D1F1F3D2C2D3A1A2';
map = [1:9 4 3 2 1 0 0]; % note that E is missing
m2 = map(hex2dec(m.'))
m2 = 1×74
1 2 0 1 2 2 4 3 3 2 3 1 2 2 4 1 2 2 0 2 1 2 3 3 2 3 4 2 0 1
  2 Kommentare
Arif Hoq
Arif Hoq am 24 Feb. 2022
how did you make this character vector m from this m ([D, 2, F, 1, C, 2, A, 3, B, 2, .........2, D, 3, A, 1, A, 2])?
DGM
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,', ','')
m = 'D2F1C2A3B2B1C2A1C2F2D2B3C3A2F1A2F1C1F2A1A1A3F1D1B2B1B2B2D3D1F1F3D2C2D3A1A2'

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by