The function move_me is defined like this: function w = move_me(v,a). The first input argument v is a row-vector, while a is a scalar. The function moves every element of v that is equal to a to the end of the vector.
1 view (last 30 days)
Show older comments
Commented: Rahul Sen on 20 Jun 2020
w=[w_1 w_1 a];
It does work for function when variable 'a' has some value but when 'a' is empty it doesn't work. Actually in the question it is also written that If 'a' is omitted, the function moves occurrences of zeros. could you please help me I am trying to figure out this problem but it has not been done. which function can be used to shift 'a' in vector 'v' to the end of vector 'v'. Thanks in advance
Rik on 19 May 2017
If this is homework, try to think of how this works and why. Add comments you wrote yourself to explain this code. Teachers know of this forum as well, so don't blindly copy stuff; it will harm you in the long run.
Rahul Sen on 20 Jun 2020
I think its combination of logical indexing and stacking. First it checks for the input "a". if it doesn't exist it assign a value "0". Next it creates two row vectors using logical indexing. First one not equals to the value "a". second one, which equals to "a". Is my understanding is right?
More Answers (4)
Jorge Briceño on 1 Feb 2018
This answer might be helpful:
function w= move_me(v,a)
% The first input argument v is a row-vector, while a is a scalar.
% The function moves every element of v that is equal to "a" to the end of the vector.
% If a is omitted, the function moves occurrences of zeros.
% Use nargin or exist to evaluate the existence of the second argument.
% If a is omitted, replace it by zero.
% Function horzcat concatenates the answer.
The built-in function "horzcat" concatenates the column vectors. In case you are not using a row vector as an input, you just need to add something like this:
As Rik Wisselink mentioned, try to think and understand what you are being asked and how the code is working. In the end, you need to develop the logic as well as programming skills.
I hope it helps.
Lars Wolff on 12 Jun 2018
It is me again....
function [w] = move_me(v,a)
v(:) = v
w = [v(v~=a) v(v==a)]
if a ~= v
w = (a==v)
but i get the message:
Problem 1 (move_me):
Feedback: Your function performed correctly for argument(s) [1 2 3 4], 2
Feedback: Your program made an error for argument(s) [0 1 2 3]
Your solution is _not_ correct.
Thanks for your help and ideas in advance!
Walter Roberson on 12 Jun 2018
Your code is testing whether a is a scalar, and it displays the result of that, but that makes no difference to the rest of the calculation.
The problem description in the original question does not say what should happen if the function is not passed any arguments, or is only called with one argument, or is called with a 2D array for the first argument, or is called with a non-scalar for the second argument.
The grading system is giving you the feedback that it passed in a single argument containing [0 1 2 3] with no second argument, and that your routine is not doing what the grading system expects in response. Your code would be failing with an error message when it tried to access the a that was not being passed in.
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!