The following Matlab project contains the source code and Matlab examples used for binary search.
IND = BSEARCH(VEC, VAL, TOL) returns indexes of VEC elements equal to VAL
VEC must be sorted in ascending order (1, 2, 3, etc)
TOL > 0 means Y = X +/- TOL counts for a match
TOL = 0 means a search for exact matches
TOL = -1 means a search for the single closest element to VAL.