The best way to ask my question is via a clear example. Consider 2 timelines (e.g. time in seconds) A and B where the intervals for each timeline are:
intervals_a =
0 1
1 4
4 7
7 9
intervals_b =
0 2
2 3
3 5
5 8
Notice that the first a-interval overlaps the first b-interval. The second a-interval overlaps the first, second, and third b-intervals, and so on.
Ultimately, I need an output which shows the indices of a-intervals which overlap with b-intervals as below:
output =
1 1 \\ 1st a-interval overlaps 1st b-interval
2 1 \\ 2nd a-interval overlaps 1st b-interval
2 2 \\ 2nd a-interval overlaps 2nd b-interval
2 3 \\ 2nd a-interval overlaps 3rd b-interval
3 3 \\ etc...
3 4
4 4
The big challenge is: The solution cannot contain for/while loops ("why" is irrelevant). Can this be done efficiently using vector / matrix / array / sort or other tools? A MATLAB implementation would be perfect, but any other language is fine. Thanks in advance!
You want the indices (ii,jj) of int_A and int_B such that int_A(ii,1) > int_B(jj,1) and int_A(ii,2)
I don't have acces to Matlab right now but I believe this is very close...
To find overlapping intervals, you need to check if the start-time or the end-time of one interval falls within the boundaries of another. To do that with for all intervals at once, you could use
bsxfun
:Example
Let's see how this works for your example:
The anonymous function
ovlp
checks if the start-times inx
(that is,x(:, 1)
) fall inside the intervals given iny
. Therefore,ovlp(intervals_a, intervals_b)
yields:The '1's indicate where start-time of interval_a falls inside interval_b. The row number is the index of the interval in
intervals_a
, and the column number is the index of the interval inintervals_b
.We need to do the same process for the start-times of
intervals_b
to find all the overlapping intervals, and we do a logical OR between the two results:Notice that the second result is transposed, to keep the rows corresponding with
intervals_a
and notintervals_b
. The resulting matrixidx
is:The final step is to translate the matrix
idx
into indices inintervals_a
andintervals_b
, so we obtain the row and column numbers of the '1's and concatenate them:The final result is: