Asked by Mr M.
on 9 Mar 2018

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?

Answer by Roger Stafford
on 9 Mar 2018

Edited by Roger Stafford
on 9 Mar 2018

Accepted 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.

Guillaume
on 9 Mar 2018

Indeed, as long as we're talking about a vector of numbers, this is the most efficient. To get the original indices of the two closest numbers:

v = randi(1000, 1, 10) %demo data

[sorted, originalidx] = sort(v);

[mindistance, where] = min(diff(sorted));

closestindex = originalidx([where, where+1]);

fprintf('elements at index %d and %d have got the minimum distance of %d\n', closestindex, mindistance)

Jan
on 9 Mar 2018

+1. Sorting at first is the cheapest approach.

Sign in to comment.

Answer by Von Duesenberg
on 9 Mar 2018

This will get you started:

dumVect = [1 3 5 30]';

[minVal, idxMin] = min(diff(dumVect))

If you work with more dimensions, you may want to use pdist instead of diff. And of course, I'll let you figure out how you want to handle ties.

Jan
on 9 Mar 2018

This is the minimal distance between neighboring elements, not between all elements.

Von Duesenberg
on 9 Mar 2018

Oops, you're right.

Sign in to comment.

Answer by Jan
on 9 Mar 2018

Edited by Jan
on 10 Mar 2018

n = 10;

v = rand(1, n);

dist = abs(v - v.'); % Auto-expand since R2016b

dist(1:(n+1):end) = Inf; % Mask the zeros [EDITED]

% dist = bsxfun(@minus, v, v.') .^ 2; % For older versions

[minValue, minIndex] = min(dist(:));

Jan
on 9 Mar 2018

@Jos: Of course 0 is the correct answer to the original question: "Any element distance from any element in the set". This includes the distance from an element to itself, which is zero. :-)

Of course, this was not meant, and I have edited the code now to solve: "Any element distance from any other element in the set".

Thanks for finding my mistake.

Mr M.
on 14 Mar 2018

What is this? v.'

Jan
on 15 Mar 2018

@Mr M.: You can simply try it.

v = rand(2,3)

v.'

It is the transpose operator. The quote without the dot before replies the conjugate complex value in addition.

Sign in to comment.

Answer by Image Analyst
on 9 Mar 2018

Sign in to comment.

Answer by Jos (10584)
on 9 Mar 2018

Edited by Jos (10584)
on 9 Mar 2018

By definition the minimum distance is zero because v(i)==v(i) for any element i of the vector v.

But I assume you want the minimum distance between v(i) and v(j) for all pairs (i,j) where i is unequal to j, but forgot to mention that ... :p

Answer by Jos (10584)
on 9 Mar 2018

Without creating a possibly large intermediate N-ny-N matrix or using a possibly slow sort

V = [1 8 6 4 2 10] ;

W = nchoose2(V) % all pairs of distinct elements

D = abs(W(:,2)-W(:,1)) % distance between pairs

[minD, ix] = min(D) % minD = 1

minPair = W(ix,:) % minPair = [1 2]

nchoose2 is a fast function to get all combinations of two elements, and can be downloaded from the Matlab File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/20144-nchoose2-x-

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Image Analyst (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/387281-how-to-find-minimal-distance-between-elements#comment_543957

Sign in to comment.