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
11 Comments
Time DescendingHello 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.
This will be helpful - https://in.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
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.
Sign in to participate