Effacer les filtres
Effacer les filtres

Breaking a number up into its powers of ten

2 vues (au cours des 30 derniers jours)
Benjamin
Benjamin le 8 Mai 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.

Réponse acceptée

Roger Stafford
Roger Stafford le 9 Mai 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

Plus de réponses (1)

John D'Errico
John D'Errico le 8 Mai 2013
help dec2base
  1 commentaire
Benjamin
Benjamin le 8 Mai 2013
I don't get it. How does that do what I asked?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by