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

20 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.
Solution Comments

1 Comment
try it

1 Comment
good problem

1 Comment
Very clever! ;)
It made me laugh, not gonna lie.

1 Comment
The problem states that the variable name is "mpg" but it is actually "MPG".

1 Comment
Nice example of using tables.

2 Comments
Guys, first of all sort the weight column of table in ascending order by sortrows function and proceed further.....
and one more thing don't forget to convert table into array by table2array function otherwise it will showing an error.
Thanks for the hint!

3 Comments
Someone can help me with this problem? On my matlab desktop it run but on the online version it doesn't works. Thanks
Hello, kindly refrain from posting the whole solution completely. You can provide hints or point out mistakes to help others, but try not to give the entire solution.
try load cars.mat

2 Comments
Meaningless
This one made me laugh. Well done!

2 Comments
Please share your full code if you have done it. It will be really helpful.
Thanks in advance
What's the function of this line?
mpg=mpg{:,:}
I tried it on Matlab desktop but I got an error. what type of variable is mpg? double?

4 Comments
I don't know why my solution is not working:
function mpg = sort_cars(N)
cars = load('cars.mat')
B = sortrows(cars,2)
mpg = B.MPG(1:N)
end
I tested it on the desktop version and it works flawlessly.
Any ideas, please?
Thanks in advance!
sortrows(cars, 2) supposed to be sortrows(cars, 4), Weight is on Column 4 I think.
Better be like this: sortrows(cars, 'Weight', 'ascend');
mpg=mpg{:,:}
Add this to your code.
Hello.The output is expected to be a column vector, whereas your code returns a table. You may try extracting the vectors from the table, like this.
function mpg = sort_cars(N)
load cars.mat
W=sortrows(cars,'Weight')
mpg = W.MPG(1:N)
end

2 Comments
Comparison is equal, why assertion is failed?
The output should be an array not a table.

3 Comments
I do not understand why this code failed assertion test even though it yields the correct answer
function mpg = sort_cars(N)
load cars.mat
sorted = sortrows (cars,4)
n = N
mpg = sorted(1:n,2)
end
The output is expected to be a column vector, whereas your code returns a table.
I tried for the solution nearly 20 ways but didn't work. finally took help from the Matlab community and compleated it
Problem Recent Solvers3915
Suggested Problems

Remove the polynomials that have positive real elements of their roots.
1044 Solvers

Remove element(s) from cell array
817 Solvers

326 Solvers

863 Solvers

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