({{, (RefLink[NearestTo, paclet:ref/NearestTo])[x]
is an operator form that yields (RefLink[Nearest, paclet:ref/Nearest])[elems,x] when applied to a list elems.}, {, (RefLink[NearestTo, paclet:ref/NearestTo])[x,n]
is an operator form that yields (RefLink[Nearest, paclet:ref/Nearest])[elems,x,n] when applied to a list elems.}, {, (RefLink[NearestTo, paclet:ref/NearestTo])[x,{n,r}]
is an operator form that yields (RefLink[Nearest, paclet:ref/Nearest])[elems,x,{n,r}] when applied to a list elems.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[NearestTo, paclet:ref/NearestTo]) works for a variety of data, including numerical, geospatial, textual and visual, as well as dates and times.

(RefLink[NearestTo, paclet:ref/NearestTo])[x][elems] is equivalent to (RefLink[NearestTo, paclet:ref/NearestTo])[elems,x].

(RefLink[NearestTo, paclet:ref/NearestTo])[x,nr][elems] is equivalent to (RefLink[NearestTo, paclet:ref/NearestTo])[elems,x,nr], where nr is an integer n or a pair {n,r}.

(RefLink[NearestTo, paclet:ref/NearestTo])[x][nf] for a (RefLink[NearestFunction, paclet:ref/NearestFunction]) object nf is equivalent to nf[x].

(RefLink[NearestTo, paclet:ref/NearestTo])[x,nr][nf] for a (RefLink[NearestFunction, paclet:ref/NearestFunction]) object nf is equivalent to nf[x,nr], where nr is an integer n or a pair {n,r}.

OpenersPrimaryExamplesSection(Spacer1)Examples  (16)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Find the element nearest to 20:

NearestTo[20][{1,2,4,8,16,32}]

Find the 3 elements nearest to 20:

NearestTo[20,3][{1,2,4,8,16,32}]

Find which element is nearest to {2,3} in 2D:

NearestTo[{2,3}][{{-1,-1},{1,-1},{1,1},{-1,1}}]

Find nearest colors:

l={,,,,,,,,,,,,,,};

NearestTo[Red,3][l]

Find nearest strings:

NearestTo["aaba",3][{"aaaa","abaa","bbbb","aaaba"}]

EditDistance[#,"aaba"]&/@{"aaaa","abaa","bbbb","aaaba"}

OpenersExampleSection1(Spacer1)Scope  (6)

Give the 3 nearest elements:

NearestTo[5,3][{1,2,3,4,5,6,7,8,9,10}]

Give the elements within radius 2:

NearestTo[5,{All,2}][{1,2,3,4,5,6,7,8,9,10}]

Give at most 3 nearest elements within radius 2:

NearestTo[5,{3,2}][{1,2,3,4,5,6,7,8,9,10}]

Apply (RefLink[NearestTo, paclet:ref/NearestTo]) to a (RefLink[NearestFunction, paclet:ref/NearestFunction]) expression:

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

NearestTo[5,{3,2}][nf]

The same result can be obtained by direct application of nf:

nf[5,{3,2}]

Return the index for the nearest string:

NearestTo["David"][{"I'm","sorry","Dave"}->"Index"]

Return an (RefLink[Association, paclet:ref/Association]) giving the string element, index and distance:

NearestTo["David"][{"I'm","sorry","Dave"}->All]

Find the 3 elements nearest to 20, simultaneously reporting the elements and their distance to 20:

NearestTo[20,3][{1,2,4,8,16,32}->{"Element","Distance"}]

Find which element is nearest to {2,3} in 2D and return the appropriate label:

NearestTo[{2,3}][{{-1,-1}->a,{1,-1}->b,{1,1}->c,{-1,1}->d}]

Compute the same result using an (RefLink[Association, paclet:ref/Association]):

NearestTo[{2,3}][<|a->{-1,-1},b->{1,-1},c->{1,1},d->{-1,1}|>]

Find the element whose density is closest to a given value:

EntityClass["Element","Density"->NearestTo[Quantity[4.5,"Grams"/"Centimeters"^3]]]

EntityList[%]

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)DistanceFunction  (1)

By default, normal Euclidean distance is used for points:

NearestTo[ {0,0}][{{1.5,.6}, {2,0},{1.25,1.25}}]

Use the (RefLink[ManhattanDistance, paclet:ref/ManhattanDistance]), which sums the length of each side:

NearestTo[{0,0},DistanceFunction->ManhattanDistance][{{1.5,.6}, {2,0},{1.25,1.25}}]

The (RefLink[ChessboardDistance, paclet:ref/ChessboardDistance]) only takes into account the dimension with the largest separation:

NearestTo[{0,0},DistanceFunction->ChessboardDistance][{{1.5,.6}, {2,0},{1.25,1.25}}]

OpenersExampleSection3(Spacer1)Applications  (3)

Find nearest words in a dictionary:

nf=Nearest[DictionaryLookup[]]

NearestTo["fish",5][nf]

Find the nearest image partition to a subimage:

i=;subimage=;

NearestTo[subimage][Flatten@ImagePartition[i,45]]

Take the polygon of Mexico:

polygon=EntityValue[(Entity["Mexico", Entity["Country","Mexico"], "Entity[\"Country\", \"Mexico\"]", "country"]),"Polygon"]

Compute the nearest of the points of that polygon from your current geo location:

nearest=NearestTo[Here][polygon[[1]]]

Draw the geodesic from your location to that point:

GeoGraphics[{Arrow[GeoPath[{Here,nearest[[1]]}]],EdgeForm[Black],polygon}]

OpenersExampleSection4(Spacer1)Properties & Relations  (2)

In the case of a tie, all nearest elements are returned in order:

NearestTo[3][{5,1,13,9,16}]

(RefLink[NearestTo, paclet:ref/NearestTo])[x][elems] effectively reverses the order of the first two arguments of (RefLink[Nearest, paclet:ref/Nearest]):

NearestTo[3][{5,1,13,9,16}]===Nearest[{5,1,13,9,16},3]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Nearest, paclet:ref/Nearest])  ▪  (RefLink[EqualTo, paclet:ref/EqualTo])  ▪  (RefLink[GeoNearest, paclet:ref/GeoNearest])  ▪  (RefLink[RegionNearest, paclet:ref/RegionNearest])  ▪  (RefLink[FeatureNearest, paclet:ref/FeatureNearest])  ▪  (RefLink[NearestFunction, paclet:ref/NearestFunction])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Knowledge Representation & Access, paclet:guide/KnowledgeRepresentationAndAccess])}, {▪(RefLinkPlain[Testing Expressions, paclet:guide/TestingExpressions])}})}})