Delete overlapping ranges between time tables

1 回表示 (過去 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 Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by