multiple digit number in to individual digits

i want to change the number 1123 in 1 1 2 3, want to split combine number into into individual numbers

 Réponse acceptée

a=1234
b=str2double(regexp(num2str(a),'\d','match'))

3 commentaires

Hi, I have a follow-up question to ask. if you have a postfix expression like 123*+ then how do you split them into individual characters. For example, a = 123*+ then i need to get b = 1 2 3 * + pls help!
a = '123*+'
for K = 1 : length(a)
fprintf('character #%d of "%s" is "%c"\n', K, a, a(K));
end
Adam Danz
Adam Danz le 29 Avr 2020
For large values such as a=11122333345555566 this will not work since num2str will convert the value to '1.112233334555557e+16'. Otherwise nice solution.

Connectez-vous pour commenter.

Plus de réponses (3)

John D'Errico
John D'Errico le 22 Juil 2014
N = 1123;
Ndigits = dec2base(N,10) - '0'
Ndigits =
1 1 2 3

3 commentaires

Adam Danz
Adam Danz le 29 Avr 2020
Modifié(e) : Adam Danz le 29 Avr 2020
Great solution. Note that N must be smaller than flintmax.
Michael Ramage
Michael Ramage le 28 Août 2020
How to do this with a floating point number?
Not difficult with a floating point number, but remember that a float is NOT an exact decimal representation of that number. But...
X = 1.2345;
dec2base(X*10000,10)
ans = '12345'
or
dec2base(X*10000,10) - '0'
ans = 1×5
1 2 3 4 5
You can even fuss around and get the decimal point in there if you want, but if you want that, then sprintf is arguably a better choice.

Connectez-vous pour commenter.

Jan
Jan le 7 Fév 2017
Modifié(e) : Jan le 14 Fév 2017
For getting the digits, a conversion to a string is an indirection. Staying at numerical values is usually faster:
N = 1123;
m = floor(log10(N)); % [EDITED] Thanks Stephen
D = mod(floor(N ./ 10 .^ (m:-1:0)), 10);

5 commentaires

Stephen23
Stephen23 le 14 Fév 2017
Modifié(e) : Stephen23 le 14 Fév 2017
Almost right, but not quite:
>> N = 10;
>> m = ceil(log10(N));
>> D = mod(floor(N ./ 10 .^ (m-1:-1:0)), 10)
D =
0
It ate my ten!!!!
Using floor not ceil works:
>> N = 10;
>> m = floor(log10(N));
>> D = mod(floor(N ./ 10 .^ (m:-1:0)), 10)
D =
1 0
Jan
Jan le 14 Fév 2017
Thanks, Stephen. I've fixed it. Perhaps I need some holidays.
Adam Danz
Adam Danz le 29 Avr 2020
Quite robust!
Jay Doshi
Jay Doshi le 25 Mar 2022
If the number is 0006, and i want all four numbers. What can I do? Because this method just gives me d = 6.
N = 6;
m = 3; % order
d = mod(floor(N ./ 10 .^ (m:-1:0)), 10)
d = 1×4
0 0 0 6

Connectez-vous pour commenter.

Ramon Villamangca
Ramon Villamangca le 20 Nov 2018
Modifié(e) : Ramon Villamangca le 20 Nov 2018
a simple single line solution:
>> num = 12345042117;
>> arrayfun(@(x) mod(floor(num/10^x),10),floor(log10(num)):-1:0)
ans =
1 2 3 4 5 0 4 2 1 1 7

3 commentaires

Jyahway Dong
Jyahway Dong le 28 Jan 2019
62229893423380308135336276614282806444486645238749
apparently this number above doesn't work for the solutions proposed above, is it simply too long for MatLab to handle?
Stephen23
Stephen23 le 28 Jan 2019
Modifié(e) : Stephen23 le 25 Mar 2022
@Jyahway Dong: If the digits are that long, you'll probably input it as a char string, anyway. That means the solution would even be much simpler:
num = '62229893423380308135336276614282806444486645238749';
num - '0'
ans = 1×50
6 2 2 2 9 8 9 3 4 2 3 3 8 0 3 0 8 1 3 5 3 3 6 2 7 6 6 1 4 2

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by