Code covered by the BSD License  

Highlights from
Range intersection

5.0
5.0 | 5 ratings Rate this file 14 Downloads (last 30 days) File Size: 1.63 KB File ID: #31753
image thumbnail

Range intersection

by

Xavier Xavier

 

10 Jun 2011 (Updated )

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

Interval Merging inspired this file.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
26 Jan 2014 Ilya

Ilya

Thanks a lot!

23 Apr 2013 Bruno Luong

Bruno Luong

Good coding, fast

20 Feb 2013 Per

Per

Really useful for me.

28 Jun 2012 Xavier Xavier

Xavier Xavier

Yes, I wanted a fast code so the input should be correct. I don't check neither that AB are sorted in ascending order.

Comment only
28 Jun 2012 Aurelien Queffurust

Aurelien Queffurust

One more test could be added:
x1 = [0 10 16 20 ]
x2 = [ 3 5 15 19 25]
-> out = 3 5 16 19

now if x1 has one more value :
x1 = [0 10 16 20 26]
you get :
Attempted to access first(2); index out of bounds because numel(first)=1.

Error in range_intersection (line 35)
if first(2)<second(1)

The test would be to check that the input vector are divisible by 2 ?

Comment only
15 Jun 2012 Aurelien Queffurust

Aurelien Queffurust

Since the bug has been fixed on 30 May 2012 , I use range_intersection . It works like a charm and is definetely faster than other FEx MergeBrackets.m (Interval merging by Bruno Luong)

30 May 2012 Xavier Xavier

Xavier Xavier

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

Comment only
21 May 2012 Shaohan Hu

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

Comment only
22 Sep 2011 Eric T

Eric T

 
08 Jul 2011 Xavier Xavier

Xavier Xavier

Yes you are right, I modified the comments

Thx

Comment only
30 Jun 2011 Brian B

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

Comment only
Updates
08 Jul 2011

Correction of an error in the input description

30 May 2012

Major modification and bug fixing

Contact us