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일

8 개 추천

x = 953
d = 100
r = mod(x, d) % 53
y = (x - r) / d % y = 9

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 24일

29 개 추천

x=953
y=num2str(x)
out=str2num(y(1))

댓글 수: 5

Bilal Almulaabd
Bilal Almulaabd 2018년 4월 4일
Thank you so much.
holyyyy u saved me
Rafael Rodríguez
Rafael Rodríguez 2021년 4월 30일
thanks!
Siddh
Siddh 2025년 4월 14일
You are my savior
Monib
Monib 대략 3시간 전
Mashkoor!

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

Thorsten
Thorsten 2013년 1월 24일

1 개 추천

x = 953;
s = num2str(x);
y1 = sscanf(s(1), '%d')
y2 = sscanf(s(2:end), '%d')
Petorr
Petorr 2019년 4월 12일

1 개 추천

c = 123.45
c = 123.4500
for d = [0.01 0.1 1 10 100]
round( (mod(c,10*d)-mod(c,d))/d )
end
ans = 5
ans = 4
ans = 3
ans = 2
ans = 1
[round() is needed because of binary precision]

댓글 수: 1

Note that this fails if the original number is negative. You need to work with abs() of the number.
c = -123.45
c = -123.4500
for d = [0.01 0.1 1 10 100]
round( (mod(c,10*d)-mod(c,d))/d )
end
ans = 5
ans = 5
ans = 6
ans = 7
ans = 8

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

태그

질문:

2013년 1월 24일

댓글:

대략 4시간 전

Community Treasure Hunt

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

Start Hunting!

Translated by