I dont understand why this function is not working.
Mostrar comentarios más antiguos
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 comentarios
Rik
el 27 de Abr. de 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
el 27 de Abr. de 2020
Sajjad Hossain
el 27 de Sept. de 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
el 27 de Sept. de 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
el 17 de Dic. de 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
el 17 de Dic. de 2022
It does mean the second column of car, which should have four columns in context
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Tables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!