Select specific digits of a number
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (3)
Azzi Abdelmalek
il 24 Gen 2013
x=953
y=num2str(x)
out=str2num(y(1))
5 Commenti
Bilal Almulaabd
il 4 Apr 2018
Thank you so much.
ushara Duldeniya vidanage
il 13 Mag 2020
holyyyy u saved me
Rafael Rodríguez
il 30 Apr 2021
thanks!
Siddh
il 14 Apr 2025
You are my savior
Monib
il 18 Apr 2026
Mashkoor!
Thorsten
il 24 Gen 2013
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 Commento
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
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!