Binary Search of Elements lying within an Interval
by Roberto Olmi
14 Feb 2010
(Updated 15 Apr 2011)
Search all elements (in a sorted vector of n elem.) that lie within an interval. It runs in log(n).
|
Watch this File
|
| File Information |
| Description |
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.
|
| MATLAB release |
MATLAB 7.6 (R2008a)
|
|
Tags for This File
|
| Everyone's Tags |
|
| Tags I've Applied |
|
| Add New Tags |
Please login to tag files.
|
| Comments and Ratings (2) |
| 14 Apr 2011 |
Igor
|
|
|
| 01 May 2011 |
Igor
|
|
|
| Updates |
| 17 Feb 2010 |
Bug fixed |
| 14 Jun 2010 |
- 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)
- Reinforced the conditions in line 93 and in line 117 in order to avoid an error when (to-1)==0 or (from+1)>length(x) |
| 15 Apr 2011 |
Fixed a bug with unsigned input arrays (e.g. uint16). Thanks to Igor Varfolomeev for the comment. |
|
Contact us at files@mathworks.com