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 NearestFunction[] 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
    Allan 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:
  • DistanceFunctionAutomaticthe distance metric to use
    MethodAutomaticmethod to use
    WorkingPrecisionAutomaticprecision to use for numeric data
  • By default, the following distance functions are used for different types of elemi:
  • Norm[#1-#2]&numeric data
    JaccardDissimilarityBoolean data
    EditDistancestrings
    ColorDistancecolors
    ImageDistanceimages
    DateDifferencedates and times
    GeoDistancegeospatial 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, DistanceFunction->f is passed to ImageDistance and ColorDistance, respectively. »
  • All images are conformed using ConformImages. With DistanceFunction->Automatic, 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:
  • MachinePrecisionuse machine-precision numbers
    puse precision p
    Automaticuse adaptive precision to resolve nearest points

Examples

open allclose all

Basic Examples  (5)

Find the element nearest to 20:

In[1]:=
Click for copyable input
Out[1]=

Find the 3 elements nearest to 20:

In[2]:=
Click for copyable input
Out[2]=

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

In[1]:=
Click for copyable input
Out[1]=

Find "nearest" strings:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Find nearest colors:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Find the nearest image partition to a subimage:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Scope  (9)

Options  (6)

Applications  (8)

Properties & Relations  (2)

Neat Examples  (1)

See Also

GeoNearest  RegionNearest  FeatureNearest  NearestNeighborGraph  DistanceMatrix  Predict  Classify  FindClusters  Norm  Sort  Quantile  Interpolation  NearestFunction  DistanceTransform

Tutorials

Introduced in 2007
(6.0)
| Updated in 2017
(11.1)