Code covered by the BSD License  

Highlights from
Binary Search of Elements lying within an Interval

5.0

5.0 | 2 ratings Rate this file 4 Downloads (last 30 days) File Size: 2.42 KB File ID: #26680

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

Great tool!, however,there's something wrong with integer inputs.

01 May 2011 Igor

Seems like there's no bugs I've mentioned now for version Updated 15 Apr 2011.

Please login to add a comment or rating.
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.

Tag Activity for this File
Tag Applied By Date/Time
binary search Roberto Olmi 16 Feb 2010 14:00:56
interval search Roberto Olmi 16 Feb 2010 14:00:56
search Roberto Olmi 16 Feb 2010 14:00:56
matching Roberto Olmi 16 Feb 2010 14:00:56
search with tolerance Roberto Olmi 17 Feb 2010 09:53:52
binary search Jeff 10 Jun 2010 12:16:43

Contact us at files@mathworks.com