Select specific digits of a number

304 次查看(过去 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))

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