For given Decimal number, calculate possible fractions

42 ビュー (過去 30 日間)
mounika
mounika 2018 年 3 月 28 日
コメント済み: mounika 2018 年 3 月 28 日
I am trying to convert a decimal number to possible fractions.
For example, if my decimal is 0.5, the possible fractions can be 1/2, 5/10, 200/400 etc.
I used the 'rat' command which will give me the least possible fraction i.e., 1/2
But is there a way where I can generate the possible fractions as above for a given range ? Any help will be highly appreciated
  1 件のコメント
David Fletcher
David Fletcher 2018 年 3 月 28 日
Presumably, you'd just be multiplying the numerator and denominator by whatever range

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 28 日
max_denom = 12345; %what is the largest denominator you permit?
[n, d] = rat(TheNumber);
iters = 1 : floor(max_denom/d);
frac_numerator = n .* iters;
frac_denoms = d .* iters;
There is a slight bit more of a challenge if the numerator or the denominator have to be "round" numbers, with the degree of challenge depending upon the rules of which "round" numbers are permitted.
  1 件のコメント
mounika
mounika 2018 年 3 月 28 日
That solves my issue! Thanks a lot Walter

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by