from
Closest
by Matt Finds the value in an array X closest to the scalar b.
closest(X,b)
function c = closest(X,b)
%CLOSEST Finds the value in an array X closest to the scalar b.
% Given a vector or matrix X, CLOSEST(X,b) finds the element in X that is
% nearest to the scalar b.
%
% Example:
%
% >> X = [-1 -2 6.14 9];
% >> b = pi;
% >> c = closest(X,b)
% c =
% 6.14
%
%
% Matt, June 2013
[~,I] = min(abs(X(:)-b));
c = X(I);
end