MATLAB Answers

1

Select specific digits of a number

Asked by Dimitris on 24 Jan 2013
Latest activity Answered by Petorr on 12 Apr 2019
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

  0 Comments

Sign in to comment.

Products

4 Answers

Answer by Evgeny Pr on 24 Jan 2013
 Accepted Answer

x = 953
d = 100
r = mod(x, d) % 53
y = (x - r) / d % y = 9

  0 Comments

Sign in to comment.


Answer by Azzi Abdelmalek
on 24 Jan 2013

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

  1 Comment

Thank you so much.

Sign in to comment.


Answer by Thorsten
on 24 Jan 2013

x = 953;
s = num2str(x);
y1 = sscanf(s(1), '%d')
y2 = sscanf(s(2:end), '%d')

  0 Comments

Sign in to comment.


Answer by Petorr on 12 Apr 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]

  0 Comments

Sign in to comment.