NearestTo
Details and Options
- NearestTo works for a variety of data, including numerical, geospatial, textual and visual, as well as dates and times.
- NearestTo[x][elems] is equivalent to NearestTo[elems,x].
- NearestTo[x,nr][elems] is equivalent to NearestTo[elems,x,nr], where nr is an integer n or a pair {n,r}.
- NearestTo[x][nf] for a NearestFunction object nf is equivalent to nf[x].
- NearestTo[x,nr][nf] for a NearestFunction object nf is equivalent to nf[x,nr], where nr is an integer n or a pair {n,r}.
- NearestTo takes the following options:
-
DistanceFunction Automatic the distance metric to use Method Automatic method to use WorkingPrecision Automatic precision to use for numerical data
Examples
open all close allBasic 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}}]l = {RGBColor[1., 0.7573220867762727, 0.8225376261211264], RGBColor[1., 0.9053403248427643, 0.21717287340883917], RGBColor[1., 0.5544801460824762, 0.12056345655596812], RGBColor[1., 0.2818404077149421, 0.1073945311994069], RGBColor[1., 0.12423838985259317, 0.19023691956664956], RGBColor[0.8, 0.4542154246540884, 0.31688034954543], RGBColor[0.8, 0.5483770742736782, 0.16977938137471082], RGBColor[0.8, 0.03163746197875539, 0.5781619271042624], RGBColor[0.8, 0.1612089376881538, 0.15737556414394493], RGBColor[0.8, 0.947661405438111, 0.8404962405438101], RGBColor[0.5, 0.8592283961197744, 0.04768022523989446], RGBColor[0.5, 0.04821552495323611, 0.9686564512239644], RGBColor[0.5, 0.5966778129134023, 0.8001648440588174], RGBColor[0.5, 0.5307402622478448, 0.7029200788120107], RGBColor[0.5, 0.6654316628707297, 0.9850955091132039]};NearestTo[Red, 3][l]NearestTo["aaba", 3][{"aaaa", "abaa", "bbbb", "aaaba"}]EditDistance[#, "aaba"]& /@ {"aaaa", "abaa", "bbbb", "aaaba"}Scope (6)
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 NearestTo to a 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 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 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[%]Options (1)
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 ManhattanDistance, which sums the length of each side:
NearestTo[{0, 0}, DistanceFunction -> ManhattanDistance][{{1.5, .6}, {2, 0}, {1.25, 1.25}}]The 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}}]Applications (3)
Find nearest words in a dictionary:
nf = Nearest[DictionaryLookup[]]NearestTo["fish", 5][nf]Find the nearest image partition to a subimage:
i = [image];subimage = [image];NearestTo[subimage][Flatten@ImagePartition[i, 45]]polygon = EntityValue[Entity["Country", "Mexico"], "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}]Properties & Relations (2)
Related Guides
History
Text
Wolfram Research (2018), NearestTo, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestTo.html.
CMS
Wolfram Language. 2018. "NearestTo." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NearestTo.html.
APA
Wolfram Language. (2018). NearestTo. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NearestTo.html
BibTeX
@misc{reference.wolfram_2026_nearestto, author="Wolfram Research", title="{NearestTo}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/NearestTo.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_nearestto, organization={Wolfram Research}, title={NearestTo}, year={2018}, url={https://reference.wolfram.com/language/ref/NearestTo.html}, note=[Accessed: 13-June-2026]}