フィルターのクリア

Breaking a number up into its powers of ten

2 ビュー (過去 30 日間)
Benjamin
Benjamin 2013 年 5 月 8 日
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.

採用された回答

Roger Stafford
Roger Stafford 2013 年 5 月 9 日
"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

その他の回答 (1 件)

John D'Errico
John D'Errico 2013 年 5 月 8 日
help dec2base
  1 件のコメント
Benjamin
Benjamin 2013 年 5 月 8 日
I don't get it. How does that do what I asked?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by