MATLAB: Overlapping time-intervals

intersectionintervalsoverlaps

I have two date-time arrays a and b.
a for start time and b for end time (above horizontal line on the picture).
What is the way to parse them and get what is beneath horizontal line on the picture?

Best Answer

Hi Rostislav
It looks like you want the union of closed intervals. Here is some code that I think does the job. When the ends of intervals have the same value, it works because the sort function is stable and preserves ordering for ties.
% Nx2 matrix of endpoints x1, x2 of intervals
x = [8 10; 2 4; 4 5; 5 7; 9 11; 15 16; 14 17; 10 12]
% -----plot it first
nline = repmat((1:size(x,1))',1,2);
plot(x',nline','o-')
ylim([-.5*nrow 1.5*nrow])
% ----- find union of intervals
x = sort(x,2);
nrow = size(x,1);
[x ind] = sort(x(:));
n = [(1:nrow) (1:nrow)]';
n = n(ind);
c = [ones(1,nrow) -ones(1,nrow)]';
c = c(ind);
csc = cumsum(c); % =0 at upper end of new interval(s)
irit = find(csc==0);
ilef = [1; irit+1];
ilef(end) = []; % no new interval starting at the very end
% y matrix is start and end points of the new intervals, y1,y2
%
% ny matrix is the corresponding indices of the start and end points
% in terms of what row of x they occurred in.
y = [x(ilef) x(irit)]
ny = [n(ilef) n(irit)]