Breaking a number up into its powers of ten

1 view (last 30 days)
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.

Accepted Answer

Roger Stafford
Roger Stafford on 9 May 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

More Answers (1)

John D'Errico
John D'Errico on 8 May 2013
help dec2base

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!