Products & Services
• Mobile Apps
• Services
Solutions
• Engineering, R&D
• Education
• Web & Software
• Finance, Statistics & Business Analysis
• Sciences
• Trends
Support & Learning
• Guided Learning
Company
• Work with Us
• Initiatives
•
BUILT-IN WOLFRAM LANGUAGE SYMBOL

# Nearest

Nearest[{elem1,elem2,},x]
gives the list of to which x is nearest.

Nearest[{elem1v1,elem2v2,},x]
gives the corresponding to the to which x is nearest.

Nearest[{elem1,elem2,}{v1,v2,},x]
gives the same result.

Nearest[{elem1,elem2,}Automatic,x]
takes the to be successive integers i.

Nearest[data,x,n]
gives the n nearest to x.

Nearest[data,x,{n,r}]
give the n or fewer nearest 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 kinds of data, including numerical, textual, and visual.
• If the are vectors or matrices of numbers, Nearest by default in effect uses the metric Norm[#1-#2]&.
• For strings, Nearest by default uses the edit distance metric.
• The following option can be given:
•  DistanceFunction Automatic the distance metric to use Method Automatic method to use
• By default the following distance functions are used:
•  SquaredEuclideanDistance numeric data JaccardDissimilarity Boolean data EditDistance strings ColorDistance colors ImageDistance images
• For images or colors and a distance function f, is passed to ImageDistance and ColorDistance, respectively. »
• Using named distance functions such as ManhattanDistance, ChessboardDistance, and EuclideanDistance can invoke special optimizations for numeric vector data.
• Possible settings for Method include and .
• 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 within radius r.

## ExamplesExamplesopen allclose all

### Basic Examples  (5)Basic Examples  (5)

Find the element nearest to 20:

 Out[1]=

Find the 3 elements nearest to 20:

 Out[2]=

Find which element is nearest to in 2D:

 Out[1]=

Find "nearest" strings:

 Out[1]=
 Out[2]=

Find nearest colors:

 Out[2]=

Find the nearest image partition to a subimage:

 Out[2]=