View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

2.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 1.17 KB File ID: #42121 Version: 1.1



Matt (view profile)


06 Jun 2013 (Updated )

Finds the value in an array X closest to the scalar b.

| Watch this File

File Information

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 =


This file inspired Near2.

Required Products MATLAB
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
12 Jun 2013 Richard Crozier

John D'Errics's excellent ipdm is more advanced, and supports lots of distance metrics, use the 'nearestto' option to get similar results to your funtion.

Comment only
07 Jun 2013 Jos (10584)

Jos (10584) (view profile)

[~,idx] = min(abs(X(:)-b))
c = X(idx)

would do.

07 Jun 2013 1.1

simpler algorithm, thanks to comment

Contact us