Select specific digits of a number

351 ビュー (過去 30 日間)
Dimitris
Dimitris 2013 年 1 月 24 日
コメント済み: Rafael Rodríguez 2021 年 4 月 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))
  3 件のコメント
ushara Duldeniya vidanage
ushara Duldeniya vidanage 2020 年 5 月 13 日
holyyyy u saved me
Rafael Rodríguez
Rafael Rodríguez 2021 年 4 月 30 日
thanks!

サインインしてコメントする。


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 Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by