Code covered by the BSD License  

Highlights from
Range intersection

5.0

5.0 | 1 rating Rate this file 22 Downloads (last 30 days) File Size: 1.63 KB File ID: #31753
image thumbnail

Range intersection

by Xavier Xavier

 

10 Jun 2011 (Updated 30 May 2012)

Mathematical intersection of range composed of a union of intervals

| Watch this File

File Information
Description

Purpose: Range/interval intersection

A and B two ranges of closed intervals written
as vectors [lowerbound1 upperbound1 lowerbound2 upperbound2]
or as matrix
[lowerbound1, lowerbound2, lowerboundn;
 upperbound1, upperbound2, upperboundn]
A and B have to be sorted in ascending order

out is the mathematical intersection A n B

EXAMPLE USAGE:
  >> out=range_intersection([1 3 5 9],[2 9])
    out = [2 3 5 9]
   >> out=range_intersection([40 44 55 58], [42 49 50 52])
   out = [42 44]

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Interval merging

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
30 Jun 2011 Brian B

Helpful function, but I believe the second form of the input should be
[lowerbound1, lowerbound2, lowerboundn;
 upperbound1, upperbound2, upperboundn]
instead of what is shown in the comments.

Compare the following:
range_intersection([1 3 4 5],[3 4])
range_intersection([1 3; 4 5],[3 4])
range_intersection([1 3; 4 5]',[3 4])

Thanks for sharing this.

-b

08 Jul 2011 Xavier Xavier

Yes you are right, I modified the comments

Thx

22 Sep 2011 BD Knight  
21 May 2012 Shaohan Hu

buggy? e.g. produces bad results on ([40 44 55 58], [42 49 50 52]); seems like you didn't make sure 'first' being ahead of 'second' for each iteration

30 May 2012 Xavier Xavier

The file has been modified to remove the bug. Now I make sure that 'first' is ahead 'second'.

Please login to add a comment or rating.
Updates
08 Jul 2011

Correction of an error in the input description

30 May 2012

Major modification and bug fixing

Tag Activity for this File
Tag Applied By Date/Time
interval Xavier Xavier 10 Jun 2011 16:19:37
intersection Xavier Xavier 10 Jun 2011 16:19:37

Contact us at files@mathworks.com