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 Comments

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.
Actually this is a MATLAB onramp Practice question. Out of 15 questions i compleated 13. Only 2 are remaning. I m not having the table cars. But it ic gien in the question that cars.mat is a file containing a table name "cars" with variable names Model, MPG, Horsepower, Weight, and Acceleration for several classic cars.
I tried for this problem for more than 2 hrs, I am unable to get it. If possiblecould you plese help.
I am talking this problem asexample and doing the solution
clc
clear all
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124; 109; 125; 117; 122];
tblA = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)
tblB=sortrows(tblA,"Weight")
top_height=tblB(1:3,"Age")
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
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.
@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.
It does mean the second column of car, which should have four columns in context

Sign in to comment.

 Accepted Answer

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 Comments

Sir in question it is given that cars.mat is a file containing a table cars with variable names Model, MPG, Horsepower, Weight, and Acceleration for several classic cars. so can we imagin that already cars is a table??
If so then this code should work. But this too is not working.
could you please help.
function mpg = sort_cars(N)
load('cars.mat')
cars=array2table('cars.mat')
cars=sortrows(cars,'Weight')
mpg =cars.MPG(1:N)
end
load('cars.mat');
S = sortrows(cars, 'Weight');
Thanks sir. It worked.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!