MATLAB: 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.

question move_me

function w=move_me(v,a)
if
w_1=v(v>a);
w_2=v(v<a);
w=[w_1 w_1 a];
end
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

Best Answer

function w=move_me(v,a)
if ~exist('a','var'),a=0;end
w=[v(v~=a) v(v==a)];
end
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.