Elements of a vector whose difference is minimum?
Show older comments
Is it possible to find the elements of a vector whose difference is minimum?
Accepted Answer
More Answers (1)
If v is the given vector,
N=length(v);
idx=nchoosek(1:N,2);
I=idx(:,1); J=idx(:,2);
D = abs( v(I) - v(J) );
imin = D==min(D);
v(I(imin)) , v( J(imin) )
Note that I, J can be re-used for further input vectors, v, that are the same length, N.
Categories
Find more on NaNs 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!