---
title: "NearestFunction"
language: "en"
type: "Symbol"
summary: "NearestFunction[data] represents a function whose values give the elements closest to an element that is supplied."
keywords: 
- dsearch
- dsearchn
canonical_url: "https://reference.wolfram.com/language/ref/NearestFunction.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Computational Geometry"
    link: "https://reference.wolfram.com/language/guide/ComputationalGeometry.en.md"
related_functions: 
  - 
    title: "Nearest"
    link: "https://reference.wolfram.com/language/ref/Nearest.en.md"
  - 
    title: "InterpolatingFunction"
    link: "https://reference.wolfram.com/language/ref/InterpolatingFunction.en.md"
  - 
    title: "FindClusters"
    link: "https://reference.wolfram.com/language/ref/FindClusters.en.md"
related_tutorials: 
  - 
    title: "Partitioning Data into Clusters"
    link: "https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.en.md#2948"
  - 
    title: "Using Nearest"
    link: "https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.en.md#10492167"
---
# NearestFunction

NearestFunction[data] represents a function whose values give the elements closest to an element that is supplied.

## Details

* ``NearestFunction`` works like ``Function``.

* ``NearestFunction`` is generated by ``Nearest[data]``.

* ``NearestFunction[…][x]`` gives the element closest to a particular element ``x``.

* ``NearestFunction[…][{x1, x2, …}]`` gives a list of the elements closest to each of the ``xi``.

* ``NearestFunction[…][x, n]`` gives the ``n`` nearest elements.

* ``NearestFunction[…][x, {n, r}]`` gives the at most ``n`` nearest elements within radius ``r`` of ``x``.

* ``NearestFunction[…][x, {All, r}]`` gives all elements within radius ``r`` of ``x``.

## Examples (4)

### Basic Examples (1)

Generate a ``NearestFunction`` :

```wl
In[1]:= nf = Nearest[{1, 3, 6, 10}]

Out[1]= NearestFunction[Hold[Nearest[{1, 3, 6, 10}]]]
```

Apply to a single element:

```wl
In[2]:= nf[5]

Out[2]= {6}
```

Apply to several elements using ``Map`` :

```wl
In[3]:= nf /@ {1, 2, 3, 4, 5, 6}

Out[3]= {{1}, {1, 3}, {3}, {3}, {6}, {6}}
```

Apply to several elements directly:

```wl
In[4]:= nf[{1, 2, 3, 4, 5, 6}]

Out[4]= {{1}, {1, 3}, {3}, {3}, {6}, {6}}
```

### Scope (3)

```wl
In[1]:= nf = Nearest[{1, 3, 6, 10}]

Out[1]= NearestFunction[Hold[Nearest[{1, 3, 6, 10}]]]
```

Give the 3 nearest elements:

```wl
In[2]:= nf[5, 3]

Out[2]= {6, 3, 1}
```

---

```wl
In[1]:= nf = Nearest[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]

Out[1]= NearestFunction[Hold[Nearest[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]]]
```

Give the elements within radius 2:

```wl
In[2]:= nf[5, {All, 2}]

Out[2]= {5, 4, 6, 3, 7}
```

Give the at most 3 nearest elements within radius 2:

```wl
In[3]:= nf[5, {3, 2}]

Out[3]= {5, 4, 6}
```

---

When ``NearestFunction`` is directly applied to multiple points, results are computed in parallel threads if possible:

```wl
In[1]:=
data = RandomReal[1, {10 ^ 5, 2}];
nf = Nearest[data];
test = RandomReal[1, {10 ^ 6, 2}];

In[2]:= AbsoluteTiming[nf[test];]

Out[2]= {0.1283, Null}
```

When the ``NearestFunction`` is applied to the points separately, results are computed serially:

```wl
In[3]:= AbsoluteTiming[Map[nf, test];]

Out[3]= {1.63097, Null}
```

## See Also

* [`Nearest`](https://reference.wolfram.com/language/ref/Nearest.en.md)
* [`InterpolatingFunction`](https://reference.wolfram.com/language/ref/InterpolatingFunction.en.md)
* [`FindClusters`](https://reference.wolfram.com/language/ref/FindClusters.en.md)

## Tech Notes

* [Partitioning Data into Clusters](https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.en.md#2948)
* [Using Nearest](https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.en.md#10492167)

## Related Guides

* [Computational Geometry](https://reference.wolfram.com/language/guide/ComputationalGeometry.en.md)

## Related Links

* [An Elementary Introduction to the Wolfram Language: Applying Functions Repeatedly](https://www.wolfram.com/language/elementary-introduction/27-applying-functions-repeatedly.html)

## History

* [Introduced in 2007 (6.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn60.en.md)