decimal to fraction conversion
Ältere Kommentare anzeigen
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 Kommentar
Nisar Ali
am 9 Mär. 2020
1170/1351
>> format
>> x
x =
0.8660
and back to /
format rational
thank you
Antworten (4)
Roger Stafford
am 22 Mär. 2016
5 Stimmen
The 'rat' function returns with an output in the form of continued fractions. To see it as a single fraction, use "format rat".
Walter Roberson
am 22 Mär. 2016
[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 Kommentare
Delany MacDonald
am 23 Mär. 2016
Walter Roberson
am 23 Mär. 2016
"The fraction is then num /dem". So
sprintf('%d/%d', num, dem)
Sara Fawal
am 8 Okt. 2018
Hello Walter I am trying to pass the NUM part of my fraction to create a changing vector name and in turn save this vector name at a .mat but I am having difficulty. Can you please help me :O)
My code is as follows:
for j=ETAC:0.05:1
[num, dem] = rat(j);
s1=num2str(num);
s2='VEC';
s3=strcat(s2,s1);
I want to pass the name of s3 to the line below
s3{1:end}=[ETA,WD,W,THETAC,FS];
I want the .mat file to have the same name as the vector above.
save('s3{1:end}.mat');
end
Please help. Thank you very much :O)
Walter Roberson
am 8 Okt. 2018
No, don't do that.
Would it be acceptable to have the mat file contain the variable with that name without first creating a workspace variable with that name?
Sara Fawal
am 9 Okt. 2018
I suppose that I can do that. More of a shortcut way of saving the variables I want into the .mat file. But how can I create a changing .mat file name in the save command.
Thank you again.
Steven Lord
am 9 Okt. 2018
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
am 9 Okt. 2018
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
am 16 Sep. 2018
Bearbeitet: Walter Roberson
am 16 Sep. 2018
format rat
1.5+4.25
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!