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
Test | Status | Code Input and Output |
---|---|---|
1 | Fail |
N = 5
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carbig.mat'));
Model = strtrim(string(Model));
cars = table(Model, MPG, Horsepower, Weight, Acceleration);
save cars.mat cars
assert(isequal(sort_cars(N),[35; 31; 39.1; 35.1; 31]));
N =
5
cars =
struct with fields:
cars: [406×5 table]
|
2 | Fail |
N = 6
load(fullfile(matlabroot, 'toolbox/stats/statsdemos', 'carsmall.mat'));
Model = strtrim(string(Model));
cars = table(Model, MPG, Horsepower, Weight, Acceleration);
save cars.mat cars
assert(isequal(sort_cars(N),[33; 29.5; 26; 29; 38; 32]));
N =
6
cars =
struct with fields:
cars: [100×5 table]
|
Remove all the words that end with "ain"
1292 Solvers
Find the two most distant points
1628 Solvers
495 Solvers
Sum the 'edge' values of a matrix
232 Solvers
281 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!