Code covered by the BSD License  

Highlights from

2.0 | 1 rating Rate this file 7 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