Cody
Follow


Mayla

Problem 44952. Find MPG of Lightest Cars

Mayla on 10 Sep 2023
Latest activity Reply by Walter Roberson on 24 Oct 2023

That's the question:
The file cars.mat contains a table named cars with variables Model, MPG, Horsepower, Weight, and Acceleration for several classic cars.
Load the MAT-file. Given an integer N, calculate the output variable mpg.
Output mpg should contain the MPG of the top N lightest cars (by Weight) in a column vector.
I wrote this code and the resulting column vector has the right values but it doesn't pass the tests. What's wrong?
function mpg = sort_cars(N)
load cars.mat
sorted=sortrows(cars,4)
mpg = sorted(1:N,2)
end
Dyuman Joshi
Dyuman Joshi on 10 Sep 2023
Hello Mayla,
The reason your code does not pass the test suite is because the variable mpg in your code is of Table data type, whereas the question expects the answer in Numeric data type.
Also, I would suggest you to check the assert() call while solving problems in Cody to see what and which type of output is expected.
Walter Roberson
Walter Roberson on 24 Oct 2023
Right.
The most common operating system these days is still Windows, and the most common disk file system on Windows is NTFS. NTFS file systems are case insensitive by default -- so if you have a stored NTFS file named cars.mat and you ask to load Cars.mat then it is likely to succeed.
MacOS file systems HFS+ and the newer APFS, also default to case insensitive -- but it is more common for users to deliberately switch them to case sensitive than is the case for Windows.
Mathworks' online systems such as MATLAB Online and MATLAB Grader run on Linux -- and Linux filesystems default to case sensitive
DGM
DGM on 24 Oct 2023
Filenames are case-sensitive.
LAWAN HARUNA
LAWAN HARUNA on 24 Oct 2023
N=4
sort_cars(N)
N = 4
Error using load
Unable to find file or directory 'Cars.mat'.
Error in sort_cars (line 3)
load Cars.mat
LAWAN HARUNA
LAWAN HARUNA on 24 Oct 2023
Im solving this problem but im getting an erro :
The file cars.mat contains a table named cars with variables Model, MPG, Horsepower, Weight, and Acceleration for several classic cars.
Load the MAT-file. Given an integer N, calculate the output variable mpg.
Output mpg should contain the MPG of the top N lightest cars (by Weight) in a column vector.
function mpg = sort_cars(N)
load Cars.mat
cars = sortrows(cars,[ "Weight"],'ascend')
mpg = cars.MPG{1:N}';
end
Dyuman Joshi
Dyuman Joshi on 25 Sep 2023
Thank you @goc3 for resolving the issue.
I did suspect it was related to the recent update, but was unable to find how so.
goc3
goc3 on 25 Sep 2023
This problem should be working again as the path in the test suite has been updated. The folder statsdemos in 2023a (and all previous versions?) was changed to statsdata in 2023b, which was recently released.
Bojun
Bojun on 24 Sep 2023
Hi Image Analyst,
The following is one of the test code, which returns the error I mentioned above:
%%
N = 5
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
Model = strtrim(string(Model));
cars = table(Model, MPG, Horsepower, Weight, Acceleration);
save cars.mat cars
assert(isequal(sort_cars(N),[35; 31; 39.1; 35.1; 31]));
Acutally the question did require us to load cars.mat, and I tried to load carbig.mat in the function. My guess is the same as Joshi.
Bojun
Image Analyst
Image Analyst on 24 Sep 2023
Exactly what line of code is trying to load carbig.mat? All I see is a line of code trying to load cars.mat.
To convert a table column(s) into an array, use table2array
s = load('carbig.mat')
s = struct with fields:
Model: [406×36 char] Origin: [406×7 char] MPG: [406×1 double] Cylinders: [406×1 double] Displacement: [406×1 double] Horsepower: [406×1 double] Weight: [406×1 double] Acceleration: [406×1 double] Model_Year: [406×1 double] cyl4: [406×5 char] org: [406×7 char] when: [406×5 char] Mfg: [406×13 char]
t = struct2table(s);
t = sortrows(t, 3, 'descend')
t = 406×13 table
Model Origin MPG Cylinders Displacement Horsepower Weight Acceleration Model_Year cyl4 org when Mfg ____________________________________ _______ ____ _________ ____________ __________ ______ ____________ __________ _____ _______ _____ _____________ citroen ds-21 pallas France NaN 4 133 115 3090 17.5 70 Four Europe Early citroen chevrolet chevelle concours (sw) USA NaN 8 350 165 4142 11.5 70 Other USA Early chevrolet ford torino (sw) USA NaN 8 351 153 4034 11 70 Other USA Early ford plymouth satellite (sw) USA NaN 8 383 175 4166 10.5 70 Other USA Early plymouth amc rebel sst (sw) USA NaN 8 360 175 3850 11 70 Other USA Early amc ford mustang boss 302 USA NaN 8 302 140 3353 8 70 Other USA Early ford volkswagen super beetle 117 Germany NaN 4 97 48 1978 20 71 Four Europe Early volkswagen saab 900s Sweden NaN 4 121 110 2800 15.4 81 Four Europe Late saab mazda glc Japan 46.6 4 86 65 2110 17.9 80 Four Japan Late mazda honda civic 1500 gl Japan 44.6 4 91 67 1850 13.8 80 Four Japan Late honda volkswagen rabbit c (diesel) Germany 44.3 4 90 48 2085 21.7 80 Four Europe Late volkswagen volkswagen pickup Germany 44 4 97 52 2130 24.6 82 Four Europe Late volkswagen volkswagen dasher (diesel) Germany 43.4 4 90 48 2335 23.7 80 Four Europe Late volkswagen volkswagen rabbit custom diesel Germany 43.1 4 90 48 1985 21.5 78 Four Europe Mid volkswagen volkswagen rabbit Germany 41.5 4 98 76 2144 14.7 80 Four Europe Late volkswagen renault lecar deluxe France 40.9 4 85 NaN 1835 17.3 80 Four Europe Late renault
nanRows = isnan(t.MPG);
t(nanRows, :) = []; % Get rid of nans.
% Print out the 10 highest MPG cars
for k = 1 : 10
fprintf('%2d : %s has an MPG of %g.\n', k, t.Model(k, :), t.MPG(k));
end
1 : mazda glc has an MPG of 46.6. 2 : honda civic 1500 gl has an MPG of 44.6. 3 : volkswagen rabbit c (diesel) has an MPG of 44.3. 4 : volkswagen pickup has an MPG of 44. 5 : volkswagen dasher (diesel) has an MPG of 43.4. 6 : volkswagen rabbit custom diesel has an MPG of 43.1. 7 : volkswagen rabbit has an MPG of 41.5. 8 : renault lecar deluxe has an MPG of 40.9. 9 : datsun 210 has an MPG of 40.8. 10 : datsun b210 gx has an MPG of 39.4.
Dyuman Joshi
Dyuman Joshi on 24 Sep 2023
Hi Bojun,
"but it seems the data cannot be found"
Yes, there seems to be an internal problem with the test suite.
I'll report this to the Cody Team so that they can check what the issue is and resolve it. I'll update you when I get the information.
For now, continue with solving other problems. Thank you for reporting the issue.
Bojun
Bojun on 24 Sep 2023
Hi Joshi,
I tried to solve this question but it seems the data cannot be found, and the error occurs as following:
function mpg = sort_cars(N)
load cars.mat
sorted = sortrows(cars, 4);
mpg = sorted(1:N,2);
mpg = mpg{:, :};
end
Error using load Unable to find file or directory '/MATLAB/toolbox/stats/statsdemos/carbig.mat'. Error in Test1 (line 3) load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
Does it mean the data is lost? I tried some example data in the MATLAB IDE but it works well, which outputs a colum vector.
How should I improve? Many Thanks.
Bojun