Delete overlapping ranges between time tables

조회 수: 3 (최근 30일)
Asrar Khaldey
Asrar Khaldey 2021년 1월 22일
답변: Satwik 2025년 4월 25일
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]

답변 (1개)

Satwik
Satwik 2025년 4월 25일
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!

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by