MATLAB: Delete overlaping intervals in matrix

matrixmatrix manipulationoverlapvector

I have the following matrix:
[100 200; 150 210; 190 300; 300 400; 410 600; 500 700]
I want to delete the intervals that overlap with previous ones.
The final matrix should be:
[100 200; 300 400; 410 600]
How can I achieve this?
Thanks in advance.

Best Answer

I expect there is a more elegant approach, but here is a straightforward way:
M = [100 200 5;
150 210 7;
205 300 9;
300 400 4;
410 600 3;
500 700 6];
intervalNumber = cumsum([true; M(2:end,1) >= M(1:end-1,2)]);
numberOfIntervals = max(intervalNumber);
output = zeros(numberOfIntervals,size(M,2));
for ni = 1:numberOfIntervals
thisInterval = M(intervalNumber==ni,:);
thisIntervalCol3 = thisInterval(:,3);
output(ni,:) = thisInterval(thisIntervalCol3==max(thisIntervalCol3),:);
end