Binary Search of Elements lying within an Interval
Search the elements that are lying within a specified interval.
Search the indexes of all elements in x (sorted vector of n elements) that lie within the interval.
The algorithm uses binary searches, thus it runs in log(n)
INPUT:
x: vector of numeric values sorted in ascending order
(e.g. 2,7,20,...120)
ref: numeric value of the reference point (center of the interval)
tol: numeric value corresponding to 1/2 of the width of the interval
The fourth input argument: numeric value (optional). Allows to define the maximum number of elements of x that can lie within the specified interval. This is useful in order to speed up the search.
OUTPUT:
indexes: indexes of elements of x which lie within the interval [ref-tol ref+tol]
If ref is not found in x then indexes is empty.
Cite As
Roberto Olmi (2026). Binary Search of Elements lying within an Interval (https://www.mathworks.com/matlabcentral/fileexchange/26680-binary-search-of-elements-lying-within-an-interval), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.4.0.0 | Fixed a bug with unsigned input arrays (e.g. uint16). Thanks to Igor Varfolomeev for the comment. |
||
| 1.3.0.0 | - Added lines 38-55 to consider the case when lower bound is less than the min(x) or the upper bound is more than the max(x)
|
||
| 1.1.0.0 | Bug fixed |
||
| 1.0.0.0 |
