Select specific digits of a number

조회 수: 343 (최근 30일)
Dimitris
Dimitris 2013년 1월 24일
댓글: Rafael Rodríguez 2021년 4월 30일
How can I select specific parts of a number?
For example if we have x=953, I want to select specifically the first digit (or the last two) and save it in another variable, so the outcome would be y=9 (or y=53)
Thanks

채택된 답변

Evgeny Pr
Evgeny Pr 2013년 1월 24일
x = 953
d = 100
r = mod(x, d) % 53
y = (x - r) / d % y = 9

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 24일
x=953
y=num2str(x)
out=str2num(y(1))
  댓글 수: 3
ushara Duldeniya vidanage
ushara Duldeniya vidanage 2020년 5월 13일
holyyyy u saved me
Rafael Rodríguez
Rafael Rodríguez 2021년 4월 30일
thanks!

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


Thorsten
Thorsten 2013년 1월 24일
x = 953;
s = num2str(x);
y1 = sscanf(s(1), '%d')
y2 = sscanf(s(2:end), '%d')

Petorr
Petorr 2019년 4월 12일
c = 123.45
for d = [0.01 0.1 1 10 100]
round( (mod(c,10*d)-mod(c,d))/d )
end
[round() is needed because of binary precision]

카테고리

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