Borrar filtros
Borrar filtros

Breaking a number up into its powers of ten

2 visualizaciones (últimos 30 días)
Benjamin
Benjamin el 8 de Mayo de 2013
Tried searching for it, but I don't even know how to phrase the question honestly.
Say I have a number, 421655 for example. I want to create a vector [4 2 1 6 5 5] from it. If Matlab has a function for it, great, but if someone wants to suggest a general algorithmic approach for this problem, I'd be even happier.

Respuesta aceptada

Roger Stafford
Roger Stafford el 9 de Mayo de 2013
"Breaking a number up into its powers of ten" is precisely what 'dec2base' and 'sprintf' undertake to do, except that the results are expressed in character strings rather than numerical digits.
However, it sounds as though you want to understand the techniques involved in this. To simplify matters suppose x is a non-negative integer and you wish to find the decimal digits that are used to express its value. Here is one technique that starts from the least digit and works up from there.
d = [];
b = true;
while b
r = mod(x,10);
x = (x-r)/10;
d = [r,d];
b = (x~=0);
end

Más respuestas (1)

John D'Errico
John D'Errico el 8 de Mayo de 2013
help dec2base
  1 comentario
Benjamin
Benjamin el 8 de Mayo de 2013
I don't get it. How does that do what I asked?

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by