I dont understand why this function is not working.
古いコメントを表示
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 件のコメント
Rik
2020 年 4 月 27 日
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
2020 年 4 月 27 日
Sajjad Hossain
2020 年 9 月 27 日
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
2020 年 9 月 27 日
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
2022 年 12 月 17 日
@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
2022 年 12 月 17 日
It does mean the second column of car, which should have four columns in context
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!