MATLAB: Elements of a vector whose difference is minimum

minimialization

Is it possible to find the elements of a vector whose difference is minimum?

Best Answer

If v is the given vector,
N=length(v);
G=abs(bsxfun(@minus,v(:),v(:).'));
G(1:N+1:end)=nan;
[i,j]=find(G==min(G(:)));
v(i), v(j)