Search value 'v' in sorted vector 'x' and find index and value
with respect to vector x that is equal or closest to 'v'.
If more than one value is equal then anyone can be returned
(this is property of binary search).
If more than one value is closest then first occurred is returned
(this is property of linear search).
First binary search is used to find v in x. If not found
then range obtained by binary search is searched linearly
to find the closest value.
x: vector of numeric values,
x should already be sorted in ascending order
v: numeric value to be search in x
i: index of v with respect to x.
cv: value that is equal or closest to v in x
Dr. Murtaza Khan (2021). Search closest value in a vector (https://www.mathworks.com/matlabcentral/fileexchange/15088-search-closest-value-in-a-vector), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!