NearestTo

NearestTo[x]

is an operator form that yields Nearest[elems,x] when applied to a list elems.

NearestTo[x,n]

is an operator form that yields Nearest[elems,x,n] when applied to a list elems.

NearestTo[x,{n,r}]

is an operator form that yields Nearest[elems,x,{n,r}] when applied to a list elems.

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}.

Examples

open allclose all

Basic Examples  (4)

Find the element nearest to 20:

Find the 3 elements nearest to 20:

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

Find nearest colors:

Find nearest strings:

Scope  (6)

Give the 3 nearest elements:

Give the elements within radius 2:

Give at most 3 nearest elements within radius 2:

Apply NearestTo to a NearestFunction expression:

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

Return the index for the nearest string:

Return an Association giving the string element, index and distance:

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

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

Compute the same result using an Association:

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

Options  (1)

DistanceFunction  (1)

By default, normal Euclidean distance is used for points:

Use the ManhattanDistance, which sums the length of each side:

The ChessboardDistance only takes into account the dimension with the largest separation:

Applications  (3)

Find nearest words in a dictionary:

Find the nearest image partition to a subimage:

Take the polygon of Mexico:

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

Draw the geodesic from your location to that point:

Properties & Relations  (2)

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

NearestTo[x][elems] effectively reverses the order of the first two arguments of Nearest:

Wolfram Research (2018), NearestTo, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestTo.html.

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_2024_nearestto, author="Wolfram Research", title="{NearestTo}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/NearestTo.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_nearestto, organization={Wolfram Research}, title={NearestTo}, year={2018}, url={https://reference.wolfram.com/language/ref/NearestTo.html}, note=[Accessed: 22-December-2024 ]}