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
Good example of tables
load cars
a = [];
c = str2double(cars.Variables);
Mg2 = c(:,2);
h1 = c(:,4);
for s = 1:N
[w,q] = min(h1,[],'omitnan');
h1(q) = max(h1)+1;
a = [a;q];
end
mpg = Mg2(a)
end
Someone can help me with this problem? On my matlab desktop it run but on the online version it doesn't works. Thanks
Meaningless
load cars
a = [];
c = str2double(cars.Variables);
Mg2 = c(:,2);
h1 = c(:,4);
for s = 1:N
[w,q] = min(h1,[],'omitnan');
h1(q) = max(h1)+1;
a = [a;q];
end
mpg = Mg2(a)
end
it does not work if u did not put curly brackets. since otherwise u call a table. however, you need a column array to pass the test I believe.
function mpg = sort_cars(N)
load cars.mat
sorted = sortrows (cars,4)
n = N
mpg = sorted(1:n,2)
mpg=mpg{:,:}
end
Please share your full code if you have done it. It will be really helpful.
Thanks in advance
What's the function of this line?
mpg=mpg{:,:}
I tried it on Matlab desktop but I got an error. what type of variable is mpg? double?
load cars.mat
sorted = sortrows (cars,4)
n = N
mpg = sorted(1:n,2)
mpg=mpg{:,:}
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.
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
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
Project Euler: Problem 6, Natural numbers, squares and sums.
1018 Solvers
348 Solvers
214 Solvers
2645 Solvers
1821 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!