I have solved the question but there is an error.....so plz give me a solution

1 回表示 (過去 30 日間)
Darshan Kanungo
Darshan Kanungo 2015 年 5 月 30 日
回答済み: Image Analyst 2015 年 5 月 30 日
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 件のコメント
Darshan Kanungo
Darshan Kanungo 2015 年 5 月 30 日
thanku...its a simple and i understand it.

サインインしてコメントする。

採用された回答

Jan
Jan 2015 年 5 月 30 日
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 件のコメント
John D'Errico
John D'Errico 2015 年 5 月 30 日
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."

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 5 月 30 日
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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by