Home > FMAToolbox > General > SubtractIntervals.m

SubtractIntervals

PURPOSE ^

SubtractIntervals - Subtract intervals.

SYNOPSIS ^

function [intervals,indices] = SubtractIntervals(intervals1,intervals2,varargin)

DESCRIPTION ^

SubtractIntervals - Subtract intervals.

 Given two lists of intervals, subtract from each interval in the first
 list its intersection with each of the intervals in the second list.

  USAGE

    [intervals,indices] = SubtractIntervals(intervals1,intervals2,<options>)

    intervals1     list of reference intervals
    intervals2     list of intervals to subtract
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'strict'      intervals with common bounds are as intersecting ('on')
                   or disjoint ('off') (default = 'off')
    =========================================================================

  OUTPUT

    intervals      resulting intervals
    indices        indices of these intervals in the original list

  SEE

    See also ConsolidateIntervals, ExcludeIntervals, InIntervals, Restrict,
    FindInInterval, CountInIntervals, PlotIntervals.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [intervals,indices] = SubtractIntervals(intervals1,intervals2,varargin)
0002 
0003 %SubtractIntervals - Subtract intervals.
0004 %
0005 % Given two lists of intervals, subtract from each interval in the first
0006 % list its intersection with each of the intervals in the second list.
0007 %
0008 %  USAGE
0009 %
0010 %    [intervals,indices] = SubtractIntervals(intervals1,intervals2,<options>)
0011 %
0012 %    intervals1     list of reference intervals
0013 %    intervals2     list of intervals to subtract
0014 %    <options>      optional list of property-value pairs (see table below)
0015 %
0016 %    =========================================================================
0017 %     Properties    Values
0018 %    -------------------------------------------------------------------------
0019 %     'strict'      intervals with common bounds are as intersecting ('on')
0020 %                   or disjoint ('off') (default = 'off')
0021 %    =========================================================================
0022 %
0023 %  OUTPUT
0024 %
0025 %    intervals      resulting intervals
0026 %    indices        indices of these intervals in the original list
0027 %
0028 %  SEE
0029 %
0030 %    See also ConsolidateIntervals, ExcludeIntervals, InIntervals, Restrict,
0031 %    FindInInterval, CountInIntervals, PlotIntervals.
0032 
0033 
0034 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0035 %
0036 % This program is free software; you can redistribute it and/or modify
0037 % it under the terms of the GNU General Public License as published by
0038 % the Free Software Foundation; either version 3 of the License, or
0039 % (at your option) any later version.
0040 
0041 % Default values
0042 strict = 'off';
0043 
0044 if nargin < 2,
0045     error('Incorrect number of parameters (type ''help <a href="matlab:help SubtractIntervals">SubtractIntervals</a>'' for details).');
0046 end
0047 
0048 if mod(length(varargin),2) ~= 0,
0049     error('Incorrect number of parameters (type ''help <a href="matlab:help SubtractIntervals">SubtractIntervals</a>'' for details).');
0050 end
0051 
0052 % Parse options
0053 for i = 1:2:length(varargin),
0054     if ~ischar(varargin{i}),
0055         error(['Parameter ' num2str(i+firstIndex) ' is not a property (type ''help <a href="matlab:help SubtractIntervals">SubtractIntervals</a>'' for details).']);
0056     end
0057     switch(lower(varargin{i})),
0058         case 'strict',
0059             strict = lower(varargin{i+1});
0060             if ~isastring(strict,'on','off'),
0061                 error('Incorrect value for property ''strict'' (type ''help <a href="matlab:help SubtractIntervals">SubtractIntervals</a>'' for details).');
0062             end
0063         otherwise,
0064             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help SubtractIntervals">SubtractIntervals</a>'' for details).']);
0065     end
0066 end
0067 
0068 indices = (1:size(intervals1,1))';
0069 for i = 1:size(intervals2,1),
0070     % Trim all intervals in intervals1 that left-intersect with intervals2(i)
0071     intersect = intervals2(i,1) <= intervals1(:,1) & intervals2(i,2) >= intervals1(:,1);
0072     if sum(intersect) ~= 0, intervals1(intersect,1) = intervals2(i,2); end
0073     % Trim all intervals in intervals1 that right-intersect with intervals2(i)
0074     intersect = intervals2(i,1) <= intervals1(:,2) & intervals2(i,2) >= intervals1(:,2);
0075     if sum(intersect) ~= 0, intervals1(intersect,2) = intervals2(i,1); end
0076     % Remove all intervals in intervals1 that are contained in intervals2(i)
0077     inside = intervals2(i,1) <= intervals1(:,1) & intervals2(i,2) >= intervals1(:,2);
0078     if sum(inside) ~= 0, intervals1(inside,:) = []; indices(inside,:) = []; end
0079     % Split all intervals in intervals1 that contain intervals2(i)
0080     contain = intervals2(i,1) >= intervals1(:,1) & intervals2(i,2) <= intervals1(:,2);
0081     n = sum(contain);
0082     if n ~= 0,
0083         first = [intervals1(contain,1) repmat(intervals2(i,1),n,1)];
0084         second = [repmat(intervals2(i,2),n,1) intervals1(contain,2)];
0085         intervals1(contain,:) = first;
0086         intervals1 = Insert(intervals1,second,find(contain));
0087         indices = Insert(indices,indices(contain),find(contain));
0088     end
0089 end
0090 intervals = intervals1;

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005