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

 Respuesta aceptada

Evgeny Pr
Evgeny Pr el 24 de En. de 2013
x = 953
d = 100
r = mod(x, d) % 53
y = (x - r) / d % y = 9

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 24 de En. de 2013

29 votos

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

5 comentarios

Bilal Almulaabd
Bilal Almulaabd el 4 de Abr. de 2018
Thank you so much.
ushara Duldeniya vidanage
ushara Duldeniya vidanage el 13 de Mayo de 2020
holyyyy u saved me
Rafael Rodríguez
Rafael Rodríguez el 30 de Abr. de 2021
thanks!
Siddh
Siddh el 14 de Abr. de 2025
You are my savior
Monib
Monib el 18 de Abr. de 2026 a las 21:38
Mashkoor!

Iniciar sesión para comentar.

Thorsten
Thorsten el 24 de En. de 2013
x = 953;
s = num2str(x);
y1 = sscanf(s(1), '%d')
y2 = sscanf(s(2:end), '%d')
Petorr
Petorr el 12 de Abr. de 2019
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 comentario

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

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 24 de En. de 2013

Comentada:

el 18 de Abr. de 2026 a las 22:50

Community Treasure Hunt

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

Start Hunting!

Translated by