multiple digit number in to individual digits

111 visualizaciones (últimos 30 días)
Raza
Raza el 22 de Jul. de 2014
Editada: John D'Errico el 27 de Feb. de 2023
i want to change the number 1123 in 1 1 2 3, want to split combine number into into individual numbers

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Jul. de 2014
a=1234
b=str2double(regexp(num2str(a),'\d','match'))
  3 comentarios
Walter Roberson
Walter Roberson el 7 de Feb. de 2017
a = '123*+'
for K = 1 : length(a)
fprintf('character #%d of "%s" is "%c"\n', K, a, a(K));
end
Adam Danz
Adam Danz el 29 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (3)

John D'Errico
John D'Errico el 22 de Jul. de 2014
N = 1123;
Ndigits = dec2base(N,10) - '0'
Ndigits =
1 1 2 3
  3 comentarios
Michael Ramage
Michael Ramage el 28 de Ag. de 2020
How to do this with a floating point number?
John D'Errico
John D'Errico el 27 de Feb. de 2023
Editada: John D'Errico el 27 de Feb. de 2023
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.

Iniciar sesión para comentar.


Jan
Jan el 7 de Feb. de 2017
Editada: Jan el 14 de Feb. de 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 comentarios
Jay Doshi
Jay Doshi el 25 de Mzo. de 2022
If the number is 0006, and i want all four numbers. What can I do? Because this method just gives me d = 6.
Stephen23
Stephen23 el 25 de Mzo. de 2022
N = 6;
m = 3; % order
d = mod(floor(N ./ 10 .^ (m:-1:0)), 10)
d = 1×4
0 0 0 6

Iniciar sesión para comentar.


Ramon Villamangca
Ramon Villamangca el 20 de Nov. de 2018
Editada: Ramon Villamangca el 20 de Nov. de 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 comentarios
Stephen23
Stephen23 el 28 de En. de 2019
Editada: Stephen23 el 25 de Mzo. de 2022
Ramon Villamangca
Ramon Villamangca el 27 de Feb. de 2023
@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

Iniciar sesión para comentar.

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by