multiple digit number in to individual digits
204 views (last 30 days)
Show older comments
i want to change the number 1123 in 1 1 2 3, want to split combine number into into individual numbers
0 Comments
Accepted Answer
Azzi Abdelmalek
on 22 Jul 2014
a=1234
b=str2double(regexp(num2str(a),'\d','match'))
3 Comments
Adam Danz
on 29 Apr 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.
More Answers (3)
Jan
on 7 Feb 2017
Edited: Jan
on 14 Feb 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);
Ramon Villamangca
on 20 Nov 2018
Edited: Ramon Villamangca
on 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
2 Comments
Stephen23
on 28 Jan 2019
@Jyahway Dong: read about floating-point numbers and their properties:
For a number with that precision you will need to use the symbolic toolbox, or VPI class, or something similar:
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!