Select specific digits of a number

133 vues (au cours des 30 derniers jours)
Dimitris
Dimitris le 24 Jan 2013
Commenté : Siddh le 14 Avr 2025
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

Réponse acceptée

Evgeny Pr
Evgeny Pr le 24 Jan 2013
x = 953
d = 100
r = mod(x, d) % 53
y = (x - r) / d % y = 9

Plus de réponses (3)

Azzi Abdelmalek
Azzi Abdelmalek le 24 Jan 2013
x=953
y=num2str(x)
out=str2num(y(1))
  4 commentaires
Rafael Rodríguez
Rafael Rodríguez le 30 Avr 2021
thanks!
Siddh
Siddh le 14 Avr 2025
You are my savior

Connectez-vous pour commenter.


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

Petorr
Petorr le 12 Avr 2019
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]

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by