# Nearest

Nearest[{elem1,elem2,},x]

gives the list of elemi to which x is nearest.

Nearest[{elem1v1,elem2v2,},x]

gives the vi corresponding to the elemi to which x is nearest.

Nearest[{elem1,elem2,}{v1,v2,},x]

gives the same result.

Nearest[{elem1,elem2,}prop,x]

gives the property prop for the elemi to which x is nearest.

Nearest[data,{x1,x2,}]

effectively gives {Nearest[data,x1],Nearest[data,x2],}.

Nearest[data,x,n]

gives the n nearest elemi to x.

Nearest[data,x,{n,r}]

gives the n or fewer nearest elemi to x that are within radius r of x.

Nearest[data]

generates a that can be applied repeatedly to different x.

# Details and Options

• Nearest works for a variety of data, including numerical, geospatial, textual, and visual, as well as dates and times.
• The data can also be given as an association. In this case, Nearest[<|key1val1,key2val2,|>] is equivalent to Nearest[{val1key1,val2key2,}].
• In Nearest[{elem1,elem2,}prop,], possible forms for prop include:
•  "Element" the elemi found to be nearest "Index" the index i of the elemi found to be nearest "Distance" the distance to the nearest elemi {prop1,prop2,…} a list of multiple forms All an association giving element, index and distance
• When Nearest returns several elements elemi, the nearest ones are given first.
• If several elements are at the same distance, they are returned in the order they appear in data.
• Nearest[data,x,{All,r}] can be used to get all elemi within radius r.
• The following options can be given:
•  DistanceFunction Automatic the distance metric to use Method Automatic method to use WorkingPrecision Automatic precision to use for numeric data
• By default, the following distance functions are used for different types of elemi:
•  Norm[#1-#2]& numeric data JaccardDissimilarity Boolean data EditDistance strings ColorDistance colors ImageDistance images DateDifference dates and times GeoDistance geospatial data
• Nearest with geospatial data uses GeoDistance to compute distances. The data can be given as a list of GeoPosition objects, or a GeoPosition containing an array of points.
• For images or colors and a distance function f, is passed to ImageDistance and ColorDistance, respectively. »
• All images are conformed using ConformImages. With , dimensionality reduction based on discrete cosine transform is applied to the set of images.
• Using Norm[#1-#2,p]& for or named distance functions such as ManhattanDistance, ChessboardDistance, and EuclideanDistance can invoke special optimizations for numeric vector data.
• Possible settings for Method include "Octree", "KDtree", and "Scan".
• Possible settings for the WorkingPrecision option are:
•  MachinePrecision use machine-precision numbers p use precision p Automatic use adaptive precision to resolve nearest points

# Examples

open allclose all

## Basic Examples(5)

Find the element nearest to 20:

 In[1]:=
 Out[1]=

Find the 3 elements nearest to 20:

 In[2]:=
 Out[2]=

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

 In[1]:=
 Out[1]=

Find "nearest" strings:

 In[1]:=
 Out[1]=
 In[2]:=
 Out[2]=

Find nearest colors:

 In[1]:=
 In[2]:=
 Out[2]=

Find the nearest image partition to a subimage:

 In[1]:=
 In[2]:=
 Out[2]=