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.
Never worked with Tables before. Does Cody offer any ways to debug?
Answer: I am able to print intermediate results.
I am getting the answer but the order of elements in my vector doesn't match the required answer. How can I get it in the required order?
I do not see why this does not work. It seems to work in the Workspace alright:
function mpg = sort_cars(N)
cars = load('cars.mat')
carsSorted = sortrows(cars,'Weight')
mpg = carsSorted.MPG(1:N)
end
@Hugo, check syntax for load in the documentation https://www.mathworks.com/help/matlab/ref/load.html
Specifically, check what to expect when assigning output from load to a variable.
ty @Pooja :)
I have been working at this for a bit and it seems like it should work, What am I missing?
function mpg = sort_cars(N)
load('cars.mat')
A = sortrows(cars,'Weight')
mpg=A(1:N,2)
end
function mpg = sort_cars(N)
load cars.mat
sorted = sortrows (cars,4);
mpg = sorted(1:N,2);
mpg{:,:}
end
Gives the correct output yet assertion failed. Why?
This is an improved version of my code but still doesn't work:
function mpg = sort_cars(N)
S = load('cars.mat');
cars = struct2table(S)
B = sortrows(cars,2)
mpg = B.MPG(1:N)
end
I don't know why my solution is not working:
function mpg = sort_cars(N)
cars = load('cars.mat')
B = sortrows(cars,2)
mpg = B.MPG(1:N)
end
I tested it on the desktop version and it works flawlessly.
Any ideas, please?
Thanks in advance!
sortrows(cars, 2) supposed to be sortrows(cars, 4), Weight is on Column 4 I think.
Better be like this: sortrows(cars, 'Weight', 'ascend');
mpg=mpg{:,:}
Add this to your code.
Comparison is equal, why assertion is failed?
The output should be an array not a table.
I do not understand why this code failed assertion test even though it yields the correct answer
function mpg = sort_cars(N)
load cars.mat
sorted = sortrows (cars,4)
n = N
mpg = sorted(1:n,2)
end
The output is expected to be a column vector, whereas your code returns a table.
I tried for the solution nearly 20 ways but didn't work. finally took help from the Matlab community and compleated it
Calculate the Levenshtein distance between two strings
362 Solvers
Get the length of a given vector
2013 Solvers
161 Solvers
Square Digits Number Chain Terminal Value (Inspired by Project Euler Problem 92)
152 Solvers
145 Solvers