MATLAB: How to find minimal distance between elements

forminimumvector

I have a vector, and I would like to find the minimal distance between element values. Any element distance from any element in the set. Is it possible to do this without a for cycle?

Best Answer

Let your vector be called v. Then do this:
d = min(diff(sort(v)));
This finds the minimum distance between any two elements of v, but it does not show the points in v where that occurs. To do that requires the use of the index returned as a second output of the 'sort' function as well as an index from the 'min' function. Let us know if that is what you want.