Home > FMAToolbox > General > ToIntervals.m

ToIntervals

PURPOSE ^

ToIntervals - Convert logical vector to a list of intervals.

SYNOPSIS ^

function intervals = ToIntervals(x,in)

DESCRIPTION ^

ToIntervals - Convert logical vector to a list of intervals.

  USAGE

    intervals = ToIntervals(x,in)

    x              values, e.g. timestamps
    in             logical vector (1 = x value is inside, 0 = x value is outside)

  NOTE

    Values can also be omitted, in which case the intervals are defined in terms
    of indices in the logical vector (see Example below).

  EXAMPLES


    x = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1];
    in = [0 0 1 1 1 0 0 0 1 1 0];
    ToIntervals(x,in)

     ans =
               0.3      0.5
               0.9        1

    ToIntervals(in)

     ans =
               3      5
               9     10

  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 = ToIntervals(x,in)
0002 
0003 %ToIntervals - Convert logical vector to a list of intervals.
0004 %
0005 %  USAGE
0006 %
0007 %    intervals = ToIntervals(x,in)
0008 %
0009 %    x              values, e.g. timestamps
0010 %    in             logical vector (1 = x value is inside, 0 = x value is outside)
0011 %
0012 %  NOTE
0013 %
0014 %    Values can also be omitted, in which case the intervals are defined in terms
0015 %    of indices in the logical vector (see Example below).
0016 %
0017 %  EXAMPLES
0018 %
0019 %
0020 %    x = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1];
0021 %    in = [0 0 1 1 1 0 0 0 1 1 0];
0022 %    ToIntervals(x,in)
0023 %
0024 %     ans =
0025 %               0.3      0.5
0026 %               0.9        1
0027 %
0028 %    ToIntervals(in)
0029 %
0030 %     ans =
0031 %               3      5
0032 %               9     10
0033 %
0034 %  SEE
0035 %
0036 %    See also ConsolidateIntervals, ExcludeIntervals, InIntervals, Restrict,
0037 %    FindInInterval, CountInIntervals, PlotIntervals.
0038 %
0039 
0040 % Copyright (C) 2010-2011 by Michaƫl Zugaro
0041 %
0042 % This program is free software; you can redistribute it and/or modify
0043 % it under the terms of the GNU General Public License as published by
0044 % the Free Software Foundation; either version 3 of the License, or
0045 % (at your option) any later version.
0046 
0047 if nargin < 1,
0048     error('Incorrect number of parameters (type ''help <a href="matlab:help ToIntervals">ToIntervals</a>'' for details).');
0049 end
0050 
0051 if nargin == 1,
0052     in = x;
0053 end
0054 
0055 if ~islvector(in),
0056     error('Incorrect logical vector (type ''help <a href="matlab:help ToIntervals">ToIntervals</a>'' for details).');
0057 end
0058 
0059 if in(end) == 1, in(end+1) = 0; end
0060 in = in(:);
0061 din = diff([0;in]);
0062 
0063 start = din == 1;
0064 stop = din == -1;
0065 
0066 intervals = [find(start) find(stop)-1];
0067 
0068 if nargin >=2,
0069     if ~isdvector(x),
0070         error('Incorrect x values (type ''help <a href="matlab:help ToIntervals">ToIntervals</a>'' for details).');
0071     end
0072     intervals = x(intervals);
0073 end

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