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
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
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
Given two strings, find the maximum overlap
534 Solvers
How to find the position of an element in a vector without using the find function
2987 Solvers
Get the elements of diagonal and antidiagonal for any m-by-n matrix
292 Solvers
300 Solvers
Given a 4x4 matrix, swap the two middle columns
519 Solvers