필터 지우기
필터 지우기

How to replace letters with numbers in matrix

조회 수: 11 (최근 30일)
Sherman Ng
Sherman Ng 2022년 2월 24일
댓글: DGM 2022년 2월 24일
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
KSSV
KSSV 2022년 2월 24일
How is your m? What class it is?
Sherman Ng
Sherman Ng 2022년 2월 24일
m is supposed to be the matrix that I have.
Also I got this error:
Unrecognized function or variable 'D'.

댓글을 달려면 로그인하십시오.

답변 (1개)

DGM
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 = '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
Arif Hoq
Arif Hoq 2022년 2월 24일
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 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,', ','')
m = 'D2F1C2A3B2B1C2A1C2F2D2B3C3A2F1A2F1C1F2A1A1A3F1D1B2B1B2B2D3D1F1F3D2C2D3A1A2'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by