decimal to fraction conversion

893 ビュー (過去 30 日間)
Delany MacDonald
Delany MacDonald 2016 年 3 月 22 日
コメント済み: Nisar Ali 2020 年 3 月 9 日
I am given the number of women in an array in a new variable that equals 74. I am also given the total number of students in a new variable that equals to 144. I did the math on Matlab
fractionWoman =
(totalWoman./ totalStudents);
the answer is .513889. How do i make this into fraction? I have tried to rat function but it gives me multiple answers so i am confused.
  1 件のコメント
Nisar Ali
Nisar Ali 2020 年 3 月 9 日
1170/1351
>> format
>> x
x =
0.8660
and back to /
format rational
thank you

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

回答 (4 件)

Roger Stafford
Roger Stafford 2016 年 3 月 22 日
The 'rat' function returns with an output in the form of continued fractions. To see it as a single fraction, use "format rat".

MHN
MHN 2016 年 3 月 23 日
format rational
74./ 144
or if you would like to have numbers
format rational
[num, dem] = rat(74./ 144);
num./dem
  1 件のコメント
Nisar Ali
Nisar Ali 2020 年 3 月 9 日
thank you sir

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


Walter Roberson
Walter Roberson 2016 年 3 月 22 日
[num, dem] = rat(totalWoman./ totalStudents);
The fraction is then num / dem .
The output of rat is different when there are two outputs than when there is only one.
  7 件のコメント
Steven Lord
Steven Lord 2018 年 10 月 9 日
Let's generate the numerator and denominator for a rational approximation to pi.
[num, den] = rat(pi);
We can build a filename using sprintf, num2str, or if you're using a sufficiently recent version of MATLAB string operations.
filename = sprintf('VEC%d_%d.mat', num, den)
filename2 = ['VEC' num2str(num) '_' num2str(den) '.mat']
filename3 = "VEC" + num + "_" + den + ".mat"
I'm going to add the name of the temporary directory tempdir to the start of the filename, so I don't write a file to your current directory.
thefullpath = fullfile(tempdir, filename)
Save a MAT-file there.
save(thefullpath, 'num')
Does it contain the variable num?
whos('-file', thefullpath)
Walter Roberson
Walter Roberson 2018 年 10 月 9 日
Instead of going the route
(dynamic variable name somehow goes here) = value
save(thefullpath, dynamic variable name goes here)
to get the resulting variable name stored in the file designated by the variable thefullpath, you can instead use something like
fieldname = sprintf('VEC%d', num);
outstruct.(fieldname) = value;
save(thefullpath, '-struct', 'outstruct')
When the -struct keyword is used, instead of the struct itself being saved, the individual fields of the struct are saved as individual variables in the .mat .

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


Kothandaraman Thanukodi
Kothandaraman Thanukodi 2018 年 9 月 16 日
編集済み: Walter Roberson 2018 年 9 月 16 日
format rat
1.5+4.25

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by