Delete overlapping ranges between time tables
    11 次查看(过去 30 天)
  
       显示 更早的评论
    
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]
0 个评论
回答(1 个)
  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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

