de Rigo, D. (2012). * Partitioning a range from a set of values: the module "create_contiguous_intervals" of the Mastrave modelling library.* In: Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling. http://mastrave.org/doc/mtv_m/create_contiguous_intervals

## Partitioning a range from a set of values: the module "create_contiguous_intervals" of the Mastrave modelling library

**Daniele de Rigo**

#### Copyright and license notice of the function create_contiguous_intervals

Copyright © 2007,2008,2009,2010,2011,2012 Daniele de Rigo

The file create_contiguous_intervals.m is part of Mastrave.

Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Mastrave. If not, see http://www.gnu.org/licenses/.

#### Function declaration

[intervals,bound_idx] = create_contiguous_intervals(boundary_set,range= [] )

#### Description

Given a vector of numbers ` boundary_set ` (and optionally the

`to be covered by the union of the intervals to be created), this module returns a two-column matrix of contiguous`

**range**`. If`

**intervals**`is omitted or empty, it is intended to be an interval composed by the minimum and the maximum value of`

**range**`. The returned`

**boundary_set**`matrix may be described as composed by two column vectors`

**intervals**`and`

**a**`which represent the intervals' endpoints:`

**b**` [a, b] = mdeal( intervals )`

The i-th ` intervals ` row defines the i-th interval whose endpoints
are respectively

`(i) and`

**a**`(i) . The first column vector`

**b**`and the second one`

**a**`will satisfy the following conditions:`

**b**` all( a < b )`
` all( a == sort(a) )`
` all( b == sort(b) )`
` all( b(1:end-1) == a(2:end) )`
` a(1) == range(1)`
` b(end) == range(2)`
` intervals == [ a b ]`

The aforementioned conditions refer to the case in which the input
argument ` range ` is passed as numeric interval.
The

`argument also accepts non-numeric values. If`

**range**`is the label '[)' (or '[['),`

**range**`is constrained to be sort(`

**boundary_set**`) whilst the last element of`

**a**`is set to exceed the maximum value of`

**b**`by the average`

**a**`difference:`

**a**` b( end ) = max( a ) + mean( diff( a ) )`

If ` range ` is the label '(]' (or ']]'),

`is constrained to be sort(`

**boundary_set**`) whilst the first element of`

**b**`is set to be the minimum value of`

**a**`minus the average`

**b**`difference:`

**b**` a( 1 ) = min( b ) - mean( diff( b ) )`

If ` range ` is the label '[]' the behavior is the same as when

`is the empty vector [].`

**range**The function optionally returns the position matrix ` bound_idx ` of each
element of

`in the matrix`

**boundary_set**`(if not belonging to`

**intervals**`,`

**boundary_set**`elements will be reported in the`

**range**`matrix as NaN). If`

**bound_idx**`limits exclude some elements of`

**range**`, they will not be used to create`

**boundary_set**`.`

**intervals**

#### Input arguments

boundary_setvector or matrix of real numbers defining the lower and upper boundary of each contiguous interval to be created. The upper boundary of the n-th interval will be by definition the lower boundary of the (n+1)-th interval.::real::range2-columns vector of real numbers defining the extreme values of the intervals to be created, i.e. the lower boundary of the firts interval and the upper boundary of the last interval. If omitted or empty, default value is: [ min(::interval|(numeric,empty)|string::(:)) max(boundary_set(:)) ] Ifboundary_setis a string label, valid labels are: range │ meaning ───────┼─────────────────────────────────────────── '[)' │ Force the first column ofrange'[[' │ to be sort(intervals) and the │ last value of the second column to │ exceed max(boundary_set) by │ the mean of the first column differences. ───────┼─────────────────────────────────────────── '(]' │ Force the second column ofboundary_set']]' │ to be sort(intervals) and the │ first value of the first column to be │ min(boundary_set) minus the mean of │ the second column differences. ───────┼─────────────────────────────────────────── '[]' │ Same as ifboundary_setvalue were the empty │ vector [].range

#### Example of usage

% Basic usage bset = [20 10 50 30 40] [ ints , bidx ] = create_contiguous_intervals( bset ) % Passing a numeric interval asvalue [ ints , bidx ] = create_contiguous_intervals( bset , [ 20 50 ] ) [ ints , bidx ] = create_contiguous_intervals( bset , [ 40 12 ] ) [ ints , bidx ] = create_contiguous_intervals( bset , [-inf 41 ] ) [ ints , bidx ] = create_contiguous_intervals( bset , [ 4 inf] ) % Borderline case: scalar or emptyrange[ ints , bidx ] = create_contiguous_intervals( [20] , [ 4 inf] ) [ ints , bidx ] = create_contiguous_intervals( [] , [ 4 inf] ) % Passing a label asboundary_setvalue [ ints , bidx ] = create_contiguous_intervals( bset , '[)' ) [ ints , bidx ] = create_contiguous_intervals( bset , '(]' ) % Borderline case: scalar or emptyrange[ ints , bidx ] = create_contiguous_intervals( [20] , '[)' ) [ ints , bidx ] = create_contiguous_intervals( [20] , '(]' ) [ ints , bidx ] = create_contiguous_intervals( [] , '[)' ) [ ints , bidx ] = create_contiguous_intervals( [] , '(]' )boundary_set

See also: find_disjoint_intervals Keywords: intervals, ordering version: 0.2.7

#### Support

The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science. You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module. Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable. However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based. Reporting a problem that you found using Mastrave may help the developer team to find a possible bug. Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems. Thank you for your collaboration.