Select specific digits of a number
이전 댓글 표시
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
채택된 답변
추가 답변 (3개)
Azzi Abdelmalek
2013년 1월 24일
x=953
y=num2str(x)
out=str2num(y(1))
댓글 수: 5
Bilal Almulaabd
2018년 4월 4일
Thank you so much.
ushara Duldeniya vidanage
2020년 5월 13일
holyyyy u saved me
Rafael Rodríguez
2021년 4월 30일
thanks!
Siddh
2025년 4월 14일
You are my savior
Monib
대략 3시간 전
Mashkoor!
Thorsten
2013년 1월 24일
x = 953;
s = num2str(x);
y1 = sscanf(s(1), '%d')
y2 = sscanf(s(2:end), '%d')
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]
댓글 수: 1
Note that this fails if the original number is negative. You need to work with abs() of the number.
c = -123.45
for d = [0.01 0.1 1 10 100]
round( (mod(c,10*d)-mod(c,d))/d )
end
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!