({{, (RefLink[NearestFunction, paclet:ref/NearestFunction])[data] 
represents a function whose values give the elements closest to an element that is supplied.}})

OpenersNotesSection(Spacer1)Details

(RefLink[NearestFunction, paclet:ref/NearestFunction]) works like (RefLink[Function, paclet:ref/Function]).

(RefLink[NearestFunction, paclet:ref/NearestFunction]) is generated by (RefLink[Nearest, paclet:ref/Nearest])[data].

(RefLink[NearestFunction, paclet:ref/NearestFunction])[…][x] gives the element closest to a particular element x.

(RefLink[NearestFunction, paclet:ref/NearestFunction])[…][{(Subscript[x, 1]),(Subscript[x, 2]),…}] gives a list of the elements closest to each of the (Subscript[x, i]).

(RefLink[NearestFunction, paclet:ref/NearestFunction])[…][x,n] gives the n nearest elements.

(RefLink[NearestFunction, paclet:ref/NearestFunction])[…][x,{n,r}] gives the at most n nearest elements within radius r of x.

(RefLink[NearestFunction, paclet:ref/NearestFunction])[…][x,{(RefLink[All, paclet:ref/All]),r}] gives all elements within radius r of x.

OpenersPrimaryExamplesSection(Spacer1)Examples  (4)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Generate a (RefLink[NearestFunction, paclet:ref/NearestFunction]):

nf=Nearest[{1,3,6,10}]

Apply to a single element:

nf[5]

Apply to several elements using (RefLink[Map, paclet:ref/Map]):

nf/@{1,2,3,4,5,6}

Apply to several elements directly:

nf[{1,2,3,4,5,6}]

OpenersExampleSection1(Spacer1)Scope  (3)

nf=Nearest[{1,3,6,10}]

Give the 3 nearest elements:

nf[5,3]

nf=Nearest[{1,2,3,4,5,6,7,8,9,10}]

Give the elements within radius 2:

nf[5,{All,2}]

Give the at most 3 nearest elements within radius 2:

nf[5,{3,2}]

When (RefLink[NearestFunction, paclet:ref/NearestFunction]) is directly applied to multiple points, results are computed in parallel threads if possible:

data=RandomReal[1,{10^5,2}];
nf=Nearest[data];
test = RandomReal[1,{10^6,2}];

AbsoluteTiming[nf[test];]

When the (RefLink[NearestFunction, paclet:ref/NearestFunction]) is applied to the points separately, results are computed serially:

AbsoluteTiming[Map[nf, test];]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Nearest, paclet:ref/Nearest])  ▪  (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction])  ▪  (RefLink[FindClusters, paclet:ref/FindClusters])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Partitioning Data into Clusters, paclet:tutorial/PartitioningDataIntoClusters])}, {▪(RefLinkPlain[Using Nearest, paclet:tutorial/UsingNearest])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Computational Geometry, paclet:guide/ComputationalGeometry])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/27-applying-functions-repeatedly.html])(WebLink[: Applying Functions Repeatedly, https://www.wolfram.com/language/elementary-introduction/27-applying-functions-repeatedly.html])}})}})