I dont understand why this function is not working.
Show older comments
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.
please help mw with the code. this is not producing correct answer.
function mpg = sort_cars(N)
load('cars.mat')
S=table(cars)
cars=sortrows(S,'Weight','ascending')
mpg =cars(1:N,'MPG');
end
6 Comments
Rik
on 27 Apr 2020
Can you share the mat file you were provided? There is a carsmall and a carbig mat file in the stats toolbox, but neither contains a table.
Prasad Reddy
on 27 Apr 2020
Sajjad Hossain
on 27 Sep 2020
function mpg = sort_cars(N)
load cars.mat
f=table2array(cars);
car=sortrows(f,4,'Ascend')
k =car(1:N,2);
mpg=str2double(k);
end
Walter Roberson
on 27 Sep 2020
table2array() can only be used if the variables are all the same datatype. It does not seem likely to me that the Model variable would be numeric whereas the other variables look like they would be numeric.
RITISH
on 17 Dec 2022
@Sajjad Hossain what does the 2 means in k=car(1:N,2) ? Does it mean 2nd column of car which isnt there and how it is related to mpg.
Walter Roberson
on 17 Dec 2022
It does mean the second column of car, which should have four columns in context
Accepted Answer
More Answers (0)
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!