필터 지우기
필터 지우기

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.

조회 수: 3 (최근 30일)
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
  댓글 수: 2
KSSV
KSSV 2017년 5월 19일
When a is empty obviously it will not work, because there is no number to split/ divide. What you expect actually?
Wasi von Deutschland
Wasi von Deutschland 2017년 5월 19일
Actually I expect how one can make a function when 'a' is omitted then 'a' should be equal to zero in this function. Means by using 'if statement' or 'for loop' This is my full question ''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. For example, the command >> x = move_me([1 2 3 4],2); makes x equal to [1 3 4 2]. If a is omitted, the function moves occurrences of zeros.''

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2017년 5월 19일
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.
  댓글 수: 3
Rik
Rik 2017년 5월 20일
You are close. You need to do some Googling (or Yahooing or Binging) on the topic of logical indexing (which is what is happening here). Also, it isn't really a second position, but a concatenation, so I would advise you to search for that as well.
Rahul Sen
Rahul Sen 2020년 6월 20일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Jorge Briceño
Jorge Briceño 2018년 2월 1일
Hi,
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 nargin<2
% If a is omitted, replace it by zero.
% Function horzcat concatenates the answer.
a=0;
w=horzcat(v(v~=a),v(v==a));
else
w=horzcat(v(v~=a),v(v==a));
end
end
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:
v=v(:)'
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.

Vaibhav Sharma
Vaibhav Sharma 2018년 6월 8일
편집: Vaibhav Sharma 2018년 6월 8일

Vaibhav Sharma
Vaibhav Sharma 2018년 6월 8일
This is the answer

Lars Wolff
Lars Wolff 2018년 6월 12일
Hi together!
It is me again....
I tried:
function [w] = move_me(v,a)
isscalar(a)
a
v(:) = v
w = [v(v~=a) v(v==a)]
if a ~= v
w = (a==v)
end
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!
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 6월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by