Products
  • Wolfram|One

    The definitive Wolfram Language and notebook experience

  • Mathematica

    The original technical computing environment

  • Notebook Assistant + LLM Kit

    All-in-one AI assistance for your Wolfram experience

  • Compute Services
  • System Modeler
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App

More mobile apps

Core Technologies of Wolfram Products

  • Wolfram Language
  • Computable Data
  • Wolfram Notebooks
  • AI & Linguistic Understanding

Deployment Options

  • Wolfram Cloud
  • wolframscript
  • Wolfram Engine Community Edition
  • Wolfram LLM API
  • WSTPServer
  • Wolfram|Alpha APIs

From the Community

  • Function Repository
  • Community Paclet Repository
  • Example Repository
  • Neural Net Repository
  • Prompt Repository
  • Wolfram Demonstrations
  • Data Repository
  • Group & Organizational Licensing
  • All Products
Consulting & Solutions

We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

  • Data & Computational Intelligence
  • Model-Based Design
  • Algorithm Development
  • Wolfram|Alpha for Business
  • Blockchain Technology
  • Education Technology
  • Quantum Computation

Wolfram Consulting

Wolfram Solutions

  • Data Science
  • Artificial Intelligence
  • Biosciences
  • Healthcare Intelligence
  • Sustainable Energy
  • Control Systems
  • Enterprise Wolfram|Alpha
  • Blockchain Labs

More Wolfram Solutions

Wolfram Solutions For Education

  • Research Universities
  • Colleges & Teaching Universities
  • Junior & Community Colleges
  • High Schools
  • Educational Technology
  • Computer-Based Math

More Solutions for Education

  • Contact Us
Learning & Support

Get Started

  • Wolfram Language Introduction
  • Fast Intro for Programmers
  • Fast Intro for Math Students
  • Wolfram Language Documentation

More Learning

  • Highlighted Core Areas
  • Demonstrations
  • YouTube
  • Daily Study Groups
  • Wolfram Schools and Programs
  • Books

Grow Your Skills

  • Wolfram U

    Courses in computing, science, life and more

  • Community

    Learn, solve problems and share ideas.

  • Blog

    News, views and insights from Wolfram

  • Resources for

    Software Developers

Tech Support

  • Contact Us
  • Support FAQs
  • Support FAQs
  • Contact Us
Company
  • About Wolfram
  • Career Center
  • All Sites & Resources
  • Connect & Follow
  • Contact Us

Work with Us

  • Student Ambassador Initiative
  • Wolfram for Startups
  • Student Opportunities
  • Jobs Using Wolfram Language

Educational Programs for Adults

  • Summer School
  • Winter School

Educational Programs for Youth

  • Middle School Camp
  • High School Research Program
  • Computational Adventures

Read

  • Stephen Wolfram's Writings
  • Wolfram Blog
  • Wolfram Tech | Books
  • Wolfram Media
  • Complex Systems

Educational Resources

  • Wolfram MathWorld
  • Wolfram in STEM/STEAM
  • Wolfram Challenges
  • Wolfram Problem Generator

Wolfram Initiatives

  • Wolfram Science
  • Wolfram Foundation
  • History of Mathematics Project

Events

  • Stephen Wolfram Livestreams
  • Online & In-Person Events
  • Contact Us
  • Connect & Follow
Wolfram|Alpha
  • Your Account
  • User Portal
  • Wolfram Cloud
  • Products
    • Wolfram|One
    • Mathematica
    • Notebook Assistant + LLM Kit
    • Compute Services
    • System Modeler
    • Finance Platform
    • Wolfram|Alpha Notebook Edition
    • Application Server
    • Enterprise Private Cloud
    • Wolfram Engine
    • Wolfram Player
    • Wolfram Cloud App
    • Wolfram Player App

    More mobile apps

    • Core Technologies
      • Wolfram Language
      • Computable Data
      • Wolfram Notebooks
      • AI & Linguistic Understanding
    • Deployment Options
      • Wolfram Cloud
      • wolframscript
      • Wolfram Engine Community Edition
      • Wolfram LLM API
      • WSTPServer
      • Wolfram|Alpha APIs
    • From the Community
      • Function Repository
      • Community Paclet Repository
      • Example Repository
      • Neural Net Repository
      • Prompt Repository
      • Wolfram Demonstrations
      • Data Repository
    • Group & Organizational Licensing
    • All Products
  • Consulting & Solutions

    We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

    WolframConsulting.com

    Wolfram Solutions

    • Data Science
    • Artificial Intelligence
    • Biosciences
    • Healthcare Intelligence
    • Sustainable Energy
    • Control Systems
    • Enterprise Wolfram|Alpha
    • Blockchain Labs

    More Wolfram Solutions

    Wolfram Solutions For Education

    • Research Universities
    • Colleges & Teaching Universities
    • Junior & Community Colleges
    • High Schools
    • Educational Technology
    • Computer-Based Math

    More Solutions for Education

    • Contact Us
  • Learning & Support

    Get Started

    • Wolfram Language Introduction
    • Fast Intro for Programmers
    • Fast Intro for Math Students
    • Wolfram Language Documentation

    Grow Your Skills

    • Wolfram U

      Courses in computing, science, life and more

    • Community

      Learn, solve problems and share ideas.

    • Blog

      News, views and insights from Wolfram

    • Resources for

      Software Developers
    • Tech Support
      • Contact Us
      • Support FAQs
    • More Learning
      • Highlighted Core Areas
      • Demonstrations
      • YouTube
      • Daily Study Groups
      • Wolfram Schools and Programs
      • Books
    • Support FAQs
    • Contact Us
  • Company
    • About Wolfram
    • Career Center
    • All Sites & Resources
    • Connect & Follow
    • Contact Us

    Work with Us

    • Student Ambassador Initiative
    • Wolfram for Startups
    • Student Opportunities
    • Jobs Using Wolfram Language

    Educational Programs for Adults

    • Summer School
    • Winter School

    Educational Programs for Youth

    • Middle School Camp
    • High School Research Program
    • Computational Adventures

    Read

    • Stephen Wolfram's Writings
    • Wolfram Blog
    • Wolfram Tech | Books
    • Wolfram Media
    • Complex Systems
    • Educational Resources
      • Wolfram MathWorld
      • Wolfram in STEM/STEAM
      • Wolfram Challenges
      • Wolfram Problem Generator
    • Wolfram Initiatives
      • Wolfram Science
      • Wolfram Foundation
      • History of Mathematics Project
    • Events
      • Stephen Wolfram Livestreams
      • Online & In-Person Events
    • Contact Us
    • Connect & Follow
  • Wolfram|Alpha
  • Wolfram Cloud
  • Your Account
  • User Portal
Wolfram Language & System Documentation Center
Nearest
  • See Also
    • GeoNearest
    • RegionNearest
    • FeatureNearest
    • NearestTo
    • NearestNeighborGraph
    • DistanceMatrix
    • Predict
    • Classify
    • FindClusters
    • Norm
    • Sort
    • Quantile
    • Interpolation
    • NearestFunction
    • DistanceTransform
  • Related Guides
    • Logic & Boolean Algebra
    • Boolean Computation
    • Computational Geometry
    • Supervised Machine Learning
    • Text Analysis
    • Natural Language Processing
    • Sequence Alignment & Comparison
    • Distance and Similarity Measures
    • Date & Time
    • Machine Learning
    • Scientific Models
    • Linguistic Data
    • Math & Counting Operations on Lists
    • Computer Vision
    • Audio Analysis
    • Audio Processing
    • Speech Computation
  • Tech Notes
    • Partitioning Data into Clusters
    • Using Nearest
    • See Also
      • GeoNearest
      • RegionNearest
      • FeatureNearest
      • NearestTo
      • NearestNeighborGraph
      • DistanceMatrix
      • Predict
      • Classify
      • FindClusters
      • Norm
      • Sort
      • Quantile
      • Interpolation
      • NearestFunction
      • DistanceTransform
    • Related Guides
      • Logic & Boolean Algebra
      • Boolean Computation
      • Computational Geometry
      • Supervised Machine Learning
      • Text Analysis
      • Natural Language Processing
      • Sequence Alignment & Comparison
      • Distance and Similarity Measures
      • Date & Time
      • Machine Learning
      • Scientific Models
      • Linguistic Data
      • Math & Counting Operations on Lists
      • Computer Vision
      • Audio Analysis
      • Audio Processing
      • Speech Computation
    • Tech Notes
      • Partitioning Data into Clusters
      • Using Nearest

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

gives the list of elemi to which x is nearest.

Nearest[{elem1v1,elem2v2,…},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
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Options  
DistanceFunction  
Method  
WorkingPrecision  
Applications  
Properties & Relations  
Neat Examples  
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page
BUILT-IN SYMBOL
  • See Also
    • GeoNearest
    • RegionNearest
    • FeatureNearest
    • NearestTo
    • NearestNeighborGraph
    • DistanceMatrix
    • Predict
    • Classify
    • FindClusters
    • Norm
    • Sort
    • Quantile
    • Interpolation
    • NearestFunction
    • DistanceTransform
  • Related Guides
    • Logic & Boolean Algebra
    • Boolean Computation
    • Computational Geometry
    • Supervised Machine Learning
    • Text Analysis
    • Natural Language Processing
    • Sequence Alignment & Comparison
    • Distance and Similarity Measures
    • Date & Time
    • Machine Learning
    • Scientific Models
    • Linguistic Data
    • Math & Counting Operations on Lists
    • Computer Vision
    • Audio Analysis
    • Audio Processing
    • Speech Computation
  • Tech Notes
    • Partitioning Data into Clusters
    • Using Nearest
    • See Also
      • GeoNearest
      • RegionNearest
      • FeatureNearest
      • NearestTo
      • NearestNeighborGraph
      • DistanceMatrix
      • Predict
      • Classify
      • FindClusters
      • Norm
      • Sort
      • Quantile
      • Interpolation
      • NearestFunction
      • DistanceTransform
    • Related Guides
      • Logic & Boolean Algebra
      • Boolean Computation
      • Computational Geometry
      • Supervised Machine Learning
      • Text Analysis
      • Natural Language Processing
      • Sequence Alignment & Comparison
      • Distance and Similarity Measures
      • Date & Time
      • Machine Learning
      • Scientific Models
      • Linguistic Data
      • Math & Counting Operations on Lists
      • Computer Vision
      • Audio Analysis
      • Audio Processing
      • Speech Computation
    • Tech Notes
      • Partitioning Data into Clusters
      • Using Nearest

Nearest

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

gives the list of elemi to which x is nearest.

Nearest[{elem1v1,elem2v2,…},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[<|key1val1,key2val2,…|>] is equivalent to Nearest[{val1key1,val2key2,…}].
  • 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:
  • DistanceFunction Automaticthe distance metric to use
    Method Automaticmethod to use
    WorkingPrecision Automaticprecision 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 all close all

Basic Examples  (5)

Find the element nearest to 20:

Find the 3 elements nearest to 20:

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

Find "nearest" strings:

Find nearest colors:

Find the nearest image partition to a subimage:

Scope  (9)

Give the 3 nearest elements:

Give the elements within radius 2:

Give at most 3 nearest elements within radius 2:

Find the nearest matrix:

Find which element is nearest to {2,3} in 2D and return the appropriate label:

Compute the same using an Association:

Return the index for the nearest string:

Return an Association giving the string element, index, and distance:

Find the 3 elements nearest to 20, simultaneously reporting the elements and their distance to 20:

For uniform random points in 3D, give the distances for the 10 nearest to the origin:

For each of the 10 nearest, give an Association including the point element, index and distance:

Create a lookup function for future use:

Find the date nearest to a given DateObject:

Find out which of these {lat,lon} points on Earth is closest to you:

Express the input as a list of separate GeoPosition objects instead:

Report simultaneously the closest point and the distance to it:

Options  (6)

DistanceFunction  (3)

By default, normal Euclidean distance is used for points:

Use the ManhattanDistance, which sums the length of each side:

The ChessboardDistance only takes into account the dimension with the largest separation:

The DistanceFunction can be given as a symbol:

Or as a pure function:

Find nearest colors using a color distance different from the default distance in ColorDistance:

Method  (2)

Compare different methods for machine-precision data:

In three dimensions, the "KDtree" method is faster:

In 20 dimensions, a simple scan is faster:

The setting Method->{"KDtree","LeafSize"->s} may be used to control the maximum number of points in any leaf of the KD tree constructed:

Plot the tree setup time:

Plot the lookup time for different points:

WorkingPrecision  (1)

Using WorkingPrecision->MachinePrecision ensures the fastest evaluation method is used:

The results may not be correct if the numbers are not all distinct to machine precision:

All the points are effectively the same to machine precision:

An appropriate higher value of precision will work:

Applications  (8)

Plot the Nearest of a list of:

Create a Voronoi diagram:

Use higher resolution:

Use a 1-norm ("taxicab distance"):

Highlight the 200 random points closest to the origin:

Use the "taxicab" metric:

Create a nearest function from all the words in a dictionary:

Look up words closest to a given word:

Go farther:

Find the outputs from running a sequence of elementary cellular automata:

Generate a complete list of outputs from all 256 elementary cellular automata:

Find which rules give outputs nearest the specified sequences:

Use a negative distance function to find the element farthest away from the given element:

Show the returned element for all values between 1 and 5:

Take the polygon of Mexico:

Compute the nearest of the points of that polygon from your current geo location:

Draw the geodesic from your location to that point:

Properties & Relations  (2)

In the case of a tie, all nearest elements are returned in order:

The single argument form of Nearest returns a NearestFunction object:

This is an optimized lookup function, which is faster than calling Nearest repeatedly:

Neat Examples  (1)

Find successive nearest words in a dictionary:

See Also

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

Function Repository: TaxonomicNearest  NearestColorName  MoleculeFingerprintSimilarity  BinarySearch

Tech Notes

    ▪
  • Partitioning Data into Clusters
  • ▪
  • Using Nearest

Related Guides

    ▪
  • Logic & Boolean Algebra
  • ▪
  • Boolean Computation
  • ▪
  • Computational Geometry
  • ▪
  • Supervised Machine Learning
  • ▪
  • Text Analysis
  • ▪
  • Natural Language Processing
  • ▪
  • Sequence Alignment & Comparison
  • ▪
  • Distance and Similarity Measures
  • ▪
  • Date & Time
  • ▪
  • Machine Learning
  • ▪
  • Scientific Models
  • ▪
  • Linguistic Data
  • ▪
  • Math & Counting Operations on Lists
  • ▪
  • Computer Vision
  • ▪
  • Audio Analysis
  • ▪
  • Audio Processing
  • ▪
  • Speech Computation

Related Links

  • Fast Introduction for Programmers: Functionals & Operators
  • An Elementary Introduction to the Wolfram Language : Machine Learning
  • An Elementary Introduction to the Wolfram Language : Applying Functions Repeatedly

History

Introduced in 2007 (6.0) | Updated in 2014 (10.0) ▪ 2016 (11.0) ▪ 2017 (11.1)

Wolfram Research (2007), Nearest, Wolfram Language function, https://reference.wolfram.com/language/ref/Nearest.html (updated 2017).

Text

Wolfram Research (2007), Nearest, Wolfram Language function, https://reference.wolfram.com/language/ref/Nearest.html (updated 2017).

CMS

Wolfram Language. 2007. "Nearest." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/Nearest.html.

APA

Wolfram Language. (2007). Nearest. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Nearest.html

BibTeX

@misc{reference.wolfram_2025_nearest, author="Wolfram Research", title="{Nearest}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/Nearest.html}", note=[Accessed: 01-March-2026]}

BibLaTeX

@online{reference.wolfram_2025_nearest, organization={Wolfram Research}, title={Nearest}, year={2017}, url={https://reference.wolfram.com/language/ref/Nearest.html}, note=[Accessed: 01-March-2026]}

Top
Introduction for Programmers
Introductory Book
Wolfram Function Repository | Wolfram Data Repository | Wolfram Data Drop | Wolfram Language Products
Top
  • Products
  • Wolfram|One
  • Mathematica
  • Notebook Assistant + LLM Kit
  • Compute Services
  • System Modeler

  • Wolfram|Alpha Notebook Edition
  • Wolfram|Alpha Pro
  • Mobile Apps

  • Wolfram Engine
  • Wolfram Player

  • Volume & Site Licensing
  • Server Deployment Options
  • Consulting
  • Wolfram Consulting
  • Repositories
  • Data Repository
  • Function Repository
  • Community Paclet Repository
  • Neural Net Repository
  • Prompt Repository

  • Wolfram Language Example Repository
  • Notebook Archive
  • Wolfram GitHub
  • Learning
  • Wolfram U
  • Wolfram Language Documentation
  • Webinars & Training
  • Educational Programs

  • Wolfram Language Introduction
  • Fast Introduction for Programmers
  • Fast Introduction for Math Students
  • Books

  • Wolfram Community
  • Wolfram Blog
  • Public Resources
  • Wolfram|Alpha
  • Wolfram Problem Generator
  • Wolfram Challenges

  • Computer-Based Math
  • Computational Thinking
  • Computational Adventures

  • Demonstrations Project
  • Wolfram Data Drop
  • MathWorld
  • Wolfram Science
  • Wolfram Media Publishing
  • Customer Resources
  • Store
  • Product Downloads
  • User Portal
  • Your Account
  • Organization Access

  • Support FAQ
  • Contact Support
  • Company
  • About Wolfram
  • Careers
  • Contact
  • Events
Wolfram Community Wolfram Blog
Legal & Privacy Policy
WolframAlpha.com | WolframCloud.com
© 2026 Wolfram
© 2026 Wolfram | Legal & Privacy Policy |
English