Problem 44952. Find MPG of Lightest Cars
Solution Stats
Problem Comments
-
37 Comments
Never worked with Tables before. Does Cody offer any ways to debug?
Answer: I am able to print intermediate results.
I am getting the answer but the order of elements in my vector doesn't match the required answer. How can I get it in the required order?
I do not see why this does not work. It seems to work in the Workspace alright:
function mpg = sort_cars(N)
cars = load('cars.mat')
carsSorted = sortrows(cars,'Weight')
mpg = carsSorted.MPG(1:N)
end
@Hugo, check syntax for load in the documentation https://www.mathworks.com/help/matlab/ref/load.html
Specifically, check what to expect when assigning output from load to a variable.
ty @Pooja :)
I have been working at this for a bit and it seems like it should work, What am I missing?
function mpg = sort_cars(N)
load('cars.mat')
A = sortrows(cars,'Weight')
mpg=A(1:N,2)
end
Good example of tables
Forgot to load the cars the first time
When I try to load the cars.mat file using
load cars
or
load cars.mat
or
load("cars.mat")
I get the error
Error using load
Unable to read file 'cars.mat'. No such file or directory.
I would appreciate help in resolving this error!
Worst test cases, if you want us to load 2 different tables then why are you loading them again in the test suite, then you didn't even pass enough arguments to the function in the test case. Thus, even when the solution have the right logic, it fails because of the poor test cases
I did,
load cars.mat
cars=sortrows(cars,4)
mpg=cars(1:N,2)
though the output in test 2 seems correct. I get incorrect assertion. Requesting Help.
The problem does not work as intended, as loading the .mat file does not work and the table is not passed to the function.
Nonetheless, the problem is solvable—one simply needs to copy some of the test-case commands into the solution function and indicate which data set is being loaded based on the input N.
The "load cars.mat" has unknown directory ang gives the below error:
"Error using load
Unable to read file 'cars.mat'. No such file or directory."
while copying the few first lines from "Test 1", it passes. however that should not be the correct way as "Test 2" has different set.
anyone knows how to load the mat file on online Matlab cody?
function mpg = sort_cars(N)
wgt=[cars.Weight]' ;
mpg1=[cars.MPG]';
mpg2=zeros(1,length(wgt));
for i=1:length(wgt)
[wgt1 inx]=max(wgt);
mpg2(end-i+1)=mpg1(inx);
wgt(inx)=0;
end
mpg = mpg2(1:N);
end
The above code is my solution, but it is not compiling can anyone help me with this..
This problem needs correction.
It uses two different "cars" sets for testing the answer. These two sets have different values for MPG.
Please, correct me if I'm wrong
I am having the same issue as 'FB' below. However, I used the code given under Test1 to load the cars.mat. The code is below.
My code passes the 1st test but not the 2nd test. Can someone please assist me.
function MPG = sort_cars(N)
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
Model = strtrim(string(Model));
cars = table(Model, MPG, Horsepower, Weight, Acceleration);
save cars.mat cars
sorted = sortrows(cars,4);
MPG = sorted(1:N,2)
MPG = MPG{:,:}
end
N=25
my code:
function mpg = sort_cars(N)
load cars.mat cars
X= sortrows(cars,4);
mpg=X(1:N,2);
end
I have tried the two tests correctly, but failed to submit…Can anyone help me…
Good Problem
When trying to use the scrap to test your function code, you could get an error saying that cars.mat file does not exist.
That was the issue I initially had when attempting to solve it.
Can anyone help me? Here's the error message when I try to simulate my code just for loading the mat-file:
Error using load
Unable to find file or directory 'cars.mat'.
The error message still persists, cars.mat isn't loading when load cars.mat is called. Could someone resolve this ??
The test does not directly load from cars.mat, but loads from another file.
Paste this into your scratch pad to work to test 1:
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
Don't try to find the actual file--just assume it's loaded into the working directory.
[The phrasing of the question is abysmal--don't overcomplicate it]
For those with problems with the directory: Don't ask me but do this hack:
function mpg = sort_cars(N)
if N==6
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carsmall.mat'));
Model = strtrim(string(Model));
cars = table(Model, MPG, Horsepower, Weight, Acceleration);
save cars.mat cars
sorted = sortrows (cars,4);
mpg = sorted(1:N,2);
mpg=mpg{:,:}
elseif 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
sorted = sortrows (cars,4);
mpg = sorted(1:N,2);
mpg=mpg{:,:}
end
end
I leave this comment for someone who are troubling in checking the answer because of the 'absence of the cars.mat'.
Copy and paste the 'test suite' code in your 'scratch pad', then you could check whether your code is work or not before you submit it. Also, you could identify the process of your code by calling variables or lines of your code.
The try code is worng. It tries to upload files in a directory that do not exist.
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.
This is still broken; the file "cars.mat" does not exist until the test runs.
I managed to pass by running the setup code from the two tests using an "if" statement based on the size of N, but the tests as intended do not work.
This too much confusing
This test not work, this problem is terrible
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
this path is not available and the mat file is not accessible
Does anyone know where the mat files are available for this problem ?
VBBV: the file is part of the Statistics Toolbox; you'll need to have that installed if you want to test your solution locally.
Christian Schroeder
Somehow carbig.mat is now able to load, but NOT carsmall.mat,
The test for carbig.mat is working fine online. Even if i install the statistics toolbox, and run the test cases locally , but when submitting the solution, the error shows path not found related to carsmall.mat online. How can i overcome this ?
VBBV: both test cases work fine for me on Cody. Perhaps your solution is not correct after all, but without seeing your solution or the exact error message it's impossible to say.
Keep in mind that you don't need to worry about where the data comes from in your solution, BTW. There will be a file called "cars.mat" in the local directory that you can load. Go by the problem description and don't fixate too much on the test cases.
Christian Schroeder: My solution is follows, when i comment the line with output variable S, in the try-catch block, it works fine, but when i uncomment it and run, it throws this error,
Error using load
Unable to find file or directory '/MATLAB/toolbox/stats/statsdemos/carsmall.mat'.
Error in sort_cars (line 7)
S=load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carsmall.mat'));
%----------------------------------------
try % 2023a (and all prior versions?)
B=load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
S=load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carsmall.mat'));
catch % 2023b
B=load(fullfile(matlabroot, 'toolbox/stats/statsdata', 'carbig.mat'));
S=load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carsmall.mat'));
end
if N==5
[b,idx1]=sort(B.Weight,'ascend');
mpg = (B.MPG(idx1(1:N)))
elseif N==6
[s,idx2]=sort(S.Weight,'ascend');
mpg = (S.MPG(idx2(1:N)))
end
%-------------------------
There's two points I'd like to make.
First: in the catch block, you're trying to load carsmall.mat from .../statsdemos rather than .../statsdata; try the latter.
Second: you don't need to load either file in your solution. All you need to do is load a file called cars.mat in the current directory, and work with whatever data it contains. Where it comes from and whether it was originally carbig.mat, carsmall.mat or something else is wholly irrelevant. The test suite will make sure this file exists and contains sensible data; your solution does not need to worry about it.
All this is indicated in the problem description, too. It may be worth rereading that, and writing your solution based on the instructions given there.
Solution Comments
Show commentsProblem Recent Solvers6412
Suggested Problems
-
1596 Solvers
-
Project Euler: Problem 7, Nth prime
1427 Solvers
-
556 Solvers
-
363 Solvers
-
967 Solvers
More from this Author13
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!