I dont understand why this function is not working.
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 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
am 27 Apr. 2020
Sajjad Hossain
am 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
am 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
am 17 Dez. 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
am 17 Dez. 2022
It does mean the second column of car, which should have four columns in context
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!