Version (1.17 KB) by Matt
Finds the value in an array X closest to the scalar b.


Updated 7 Jun 2013

View License

Given a vector or matrix X, CLOSEST(X,b) finds the element in X that is nearest to the scalar b.


>> X = [-1 -2 6.14 9];
>> b = pi;
>> c = closest(X,b)

c =

Cite As

Matt (2023). Closest (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Linear Algebra in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

simpler algorithm, thanks to comment