Delete overlapping ranges between time tables

2 views (last 30 days)
Asrar Khaldey
Asrar Khaldey on 22 Jan 2021
Answered: Satwik on 25 Apr 2025
Hi
I have two time tables with start and end points, for example:
A=[1 5; 6 8;10 13;14 17]
B=[2 3;8 12;18 20]
I want to delete the overlapping ranges from table A
for example the result would be:
C=[1 2;3 5;6 8;12 13;14 17]

Answers (1)

Satwik
Satwik on 25 Apr 2025
Hi,
This is a classic interval subtraction problem. For each interval in 'A', remove any overlaps with intervals in 'B'.
Below is a MATLAB solution that works for the case described:
A = [1 5; 6 8; 10 13; 14 17];
B = [2 3; 8 12; 18 20];
C = [];
for i = 1:size(A,1)
seg_start = A(i,1);
seg_end = A(i,2);
intervals = [seg_start seg_end];
for j = 1:size(B,1)
b_start = B(j,1);
b_end = B(j,2);
% No overlap
if b_end <= intervals(1) || b_start >= intervals(2)
continue;
end
% Overlap at the start
if b_start > intervals(1) && b_start < intervals(2)
intervals = [intervals(1) b_start; b_start intervals(2)];
end
% Overlap at the end
if b_end > intervals(1) && b_end < intervals(2)
intervals = [intervals(1) b_end; b_end intervals(2)];
end
% Remove the overlap
mask = ~(intervals(:,1) < b_end & intervals(:,2) > b_start);
intervals = intervals(mask,:);
end
C = [C; intervals];
end
% Remove zero or negative-length intervals (if any)
C = C(C(:,2) > C(:,1), :);
disp(C);
I hope this helps!

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!