function v=neighbor(u) %Input u is a row vector %Output v is a row vector of length one less than u %Example: u = [1 2 4 7] % v = neighbor(u) l=length(u); %number of elements in u [m,n]=size(u); %dimensions of u if m==1 %condition check if u is a row vector v=zeros(1,l-1); for i=1:l-1 v(i)=abs(u(i)-u(i+1)); %diff between consecutive elements of u end else v=[]; end
Search Program on this blog
Wednesday, 19 August 2015
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment