Consider a matrix of dimension Nx2
, where each row contains the lower and upper bound of a uniform PDF (i.e., probability density function).
I want to count the number of overlaps, where an overlap is defined as the condition in which two PDFs are overlapping, e.g.:
- PDF1:
[2,5]
- PDF2:
[3,6]
- The two PDFs are overlapping in the interval
[3,5]
.
Obviously, if three PDFs p1
, p2
and p3
are overlapping, I count three overlaps: p1
vs. p2
, p1
vs. p3
, p2
vs. p3
.
I created the following MATLAB code that counts the overlaps:
for m = 1:N-1
for k = m+1:N
l1 = dataService.getObjectCoordinate(m,1);
l2 = dataService.getObjectCoordinate(k,1);
u1 = dataService.getObjectCoordinate(m,2);
u2 = dataService.getObjectCoordinate(k,2);
if (l1 <= l2 && l2 <= u1) || (l2 <= l1 && l1 <= u2)
numOverlaps = numOverlaps + 1;
end
end
end
However, as you can imagine, this goes as O(N^2)
, which is reeeeeally bad when N
is big. I started the execution three hours ago with N=10000
and it is still running.
May you suggest a way of reducing the complexity of the proposed algorithm, maybe excluding some of the comparisons a priori?
Thanks in advance.