Problem 44952. Find MPG of Lightest Cars
Solution Stats
Problem Comments

29 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 testcase 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(endi+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 matfile:
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 filejust assume it's loaded into the working directory.
[The phrasing of the question is abysmaldon'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.
Solution Comments
Show commentsProblem Recent Solvers5526
Suggested Problems

5572 Solvers

7684 Solvers

Arrange vector in ascending order
750 Solvers

440 Solvers

920 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!