This solution is outdated. To rescore this solution, sign in.
-
5 Comments
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.
Hello.The output is expected to be a column vector, whereas your code returns a table. You may try extracting the vectors from the table, like this.
function mpg = sort_cars(N)
load cars.mat
W=sortrows(cars,'Weight')
mpg = W.MPG(1:N)
end
Hello commenters. I just want to ask if you encountered error in loading the mat file. I can't run my code because when I use load cars.mat there's an issue "Unable to find file or directory 'cars.mat'."
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!