You can use pdist2
to compute all distances and then pick the minimal distance.
allDist = squareform( pdist2( set1, set2 ) );
[minDist nni] = min( allDist, [], 2 );
Now minDist
holds the minimal distance of each point in set1
to its nearest-neighbor (set2(nni)
).
EDIT:
for low dimensional points (3 in this example) it should be more efficient to look at k-NN algorithms, as proposed in my other answer.