I have solved the question but there is an error.....so plz give me a solution
3 views (last 30 days)
Show older comments
Write a function called neighbor that takes as input a row vector called v and creates another row vector as output that contains the absolute values of the differences between neighboring elements of v. For example, if v == [1 2 4 7], then the output of the function would be [1 2 3]. Notice that the length of the output vector is one less than that of the input. Check that the input v is indeed a vector and has at least two elements and return an empty array otherwise. You are not allowed to use the diff built-‐in function.
function d = neighbor(v)
for ii = 1:length(v)-1
d(ii) = abs(v(ii+1) - v(ii));
end
end
Feedback: Your function performed correctly for argument(s) [1 2 3 4]
Feedback: Your function performed correctly for argument(s) [2 1]
Feedback: Your function performed correctly for argument(s) [1 -2 3]
Feedback: Your function performed correctly for argument(s) [5 -3 6 -7 1 4 -4 6 -5 -1]
Feedback: Your function performed correctly for argument(s) [0.775665774190202 0.46043860784642 0.637754980094595 0.21592913366455 0.0321676971373623 0.777337876450925 0.979770397784413 0.221076297004035 0.063923583412422 0.129378479212439]
Feedback: Your function made an error for argument(s) [1 2;3 4]
Your solution is _not_ correct.
2 Comments
Jan
on 30 May 2015
Did you search in the internet to find a solution? You would find http://www.mathworks.com/matlabcentral/answers/219445-how-to-make-a-function-that-calculate-consective-difference-of-elements-of-a-vector
Accepted Answer
Jan
on 30 May 2015
The question is: "a function [...] that takes as input a row vector [...]". But [1 2;3 4] is not a row vector. Therefore your function is correct, but the feedback test is wrong.
So what do you want to do? Do you want to create a function which satisfies the question or the automatic test?
1 Comment
John D'Errico
on 30 May 2015
As defined by the specs:
"Check that the input v is indeed a vector and has at least two elements and return an empty array otherwise."
More Answers (1)
Image Analyst
on 30 May 2015
The problem statement is bad and should be corrected by the instructor. And you can tell him I said that.
The statements "takes as input a row vector" and "Check that the input v is indeed a vector" are ambiguous. Checking that v is a vector, with the isvector() function, is not sufficient to make sure that you passed in a row vector, since a column vector will also "pass". So if you passed in a column vector and checked if it's a vector, that would pass that requirement, then the first requirement would not be satisfied - hence the ambiguity/contradiction. For it to be consistent, the second test should say "Check that the input v is indeed a row vector" . For that, you can use the function isrow() rather than is vector. A user passing in a column vector would not pass the test and you should throw up a warning message with warndlg().
if ~isrow(v)
% v is a multi-dimensional array or a column vector.
uiwait(warndlg('Error: v is not a row vector'));
return;
end
0 Comments
See Also
Categories
Find more on Logical 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!