Nearest¶
Find the nearest elements in a list to a given value.
A count limits how many of the closest elements are returned.
Strings are compared by edit distance.
An empty point set is unusable rather than a set in which nothing happens to be near, so it is reported and the call is left as written:
$ wo 'Nearest[{}, 1]'
Nearest::near1: {} is neither a list of real points nor a valid list of rules.
Nearest[{}, 1]
The same goes for data that is not a list of points at all:
$ wo 'Nearest[5, 1]'
Nearest::near1: 5 is neither a list of real points nor a valid list of rules.
Nearest[5, 1]
DistanceFunction -> f measures with f[element, target] instead of the
built-in metric, so a named metric picks the neighbour:
A pure function works too — negating the distance asks for the farthest element instead of the nearest:
It applies through the other forms as well, here with a count:
Any other option is accepted and changes nothing: