# Wolfram Language & System 11.0 (2016)|Legacy Documentation

This is documentation for an earlier version of the Wolfram Language.
BUILT-IN WOLFRAM LANGUAGE SYMBOL

# 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,}Automatic,x]
takes the vi to be successive integers i.

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 OptionsDetails 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[{val1,val2,}->{key1,key2,}].
• When several elements are returned, 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

## ExamplesExamplesopen allclose all

### Basic Examples  (5)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]=

## TutorialsTutorials

Introduced in 2007
(6.0)
| Updated in 2015
(10.1)