I dont understand why this function is not working.

5 ビュー (過去 30 日間)
Prasad Reddy
Prasad Reddy 2020 年 4 月 27 日
コメント済み: Walter Roberson 2022 年 12 月 17 日
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 件のコメント
RITISH
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
Walter Roberson 2022 年 12 月 17 日
It does mean the second column of car, which should have four columns in context

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 27 日
S=table(cars)
That will not create any variable in table S that is named "Weight'.
If cars is already a table, then the result of that is that S would be a table with a single variable named cars and that single variable would be a table.
If cars is a numeric array, then you would want to use array2table() and you would want to pass in a 'VariableNames' option listing the variable names.
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 4 月 27 日
load('cars.mat');
S = sortrows(cars, 'Weight');
Prasad Reddy
Prasad Reddy 2020 年 4 月 27 日
Thanks sir. It worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by