## The module "cellstr2index" of the Mastrave modelling library

Daniele de Rigo

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

The file cellstr2index.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

[ indices, cardinality, glossary ] =
cellstr2index( cellstring      ,
mode       = [] )



#### Description

Strings cannot be used as indices. @cellstr2index maps strings into positive integers preserving their order. If cellstring is a cell-array of cellstrings then indices is returned as a cell-array whose elements are the corresponding matrices of indices. Each index of indices represents a value of (the elements of) cellstring which may occur several times in cellstring . To each occurrence of the same value corresponds the same index. The set of these correspondences is returned in the cell of strings glossary , whose length is the cardinality of the indices set. The particular way in which indices are generates can be governed using the optional mode input argument (if omitted, its default value is 'ascending').

#### Input arguments


cellstring              ::cellstring-1|cellstring-2::
Cell array of strings or of string cell arrays.

mode                    ::string::
Criterion for generating indices from
cellstring . The default value is [] (empty)
If empty, the mode 'ascending' is used.
Valid modes are:

mode     │      meaning
──────────────┼─────────────────────────────────
'ascending'  │ Indices are generated by
│ associating to each element of
│  cellstring  the position of its
│ first occurrence in the set of
│  cellstring  elements sorted in
│ ascending order.
──────────────┼─────────────────────────────────
'descending' │ Indices are generated by
│ associating to each element of
│  cellstring  the position of its
│ first occurrence in the set of
│  cellstring  elements sorted in
│ descending order.
──────────────┼─────────────────────────────────
'forward'    │ Indices are generated by
│ associating to each element of
│  cellstring  the position of its
│ first occurrence in the set of
│  cellstring  elements.
──────────────┼─────────────────────────────────
'backward'   │ Indices are generated by
│ associating to each element of
│  cellstring  the position of its
│ first occurrence in the set of
│  cellstring  elements listed
│ in reverse order.
──────────────┼─────────────────────────────────
'random'     │ Indices are generated by
│ associating to each element of
│  cellstring  the position of its
│ first occurrence in the set of
│  cellstring  elements randomly
│ shuffled.



#### Example of usage


% Basic usage: passing a cell-array of strings
m1            =  { 'foo'    , 'bar' , ''     , 'baz' }
m2            =  { 'foobar' , 'bar' , 'quux' , 'foo' , 'bar' }
m3            =  { }
m4            =  { 'foo'    , 'bar' ; ''     , 'baz' }
m5            =  { ''       , 'foo' ; 'foo'  , ''    }
[idx, len, g] = cellstr2index( m1 )
[idx, len, g] = cellstr2index( m2 )
[idx, len, g] = cellstr2index( m3 )
[idx, len, g] = cellstr2index( m4 )
[idx, len, g] = cellstr2index( m5 )

% How  mode  affects the generated indices
idx           = cellstr2index( m2 , 'ascending'  )
idx           = cellstr2index( m2 , 'descending' )
idx           = cellstr2index( m2 , 'forward'    )
idx           = cellstr2index( m2 , 'backward'   )
idx           = cellstr2index( m2 , 'random'     )
idx           = cellstr2index( m2 , 'random'     )

% Passing a cell-array of string cell-arrays
[idx, len]    = cellstr2index( { m1 , m2 } )
[idx, len]    = cellstr2index( { m1 , m2 , m3 } )
[idx, len]    = cellstr2index( { m1 , m2 , m4 } )
[idx, len, g] = cellstr2index( { m1 , m2 , m3 , m4 } )
[idx, len, g] = cellstr2index( { m1 , m2 , m3 , 'foo' , m4 , '' } )

% How  mode  affects the generated indices in case
% of cell-array of string cell-arrays
idx           = cellstr2index( { m1 , m2 , m3 } , 'ascending'  )
idx           = cellstr2index( { m1 , m2 , m3 } , 'descending' )
idx           = cellstr2index( { m1 , m2 , m3 } , 'forward'    )
idx           = cellstr2index( { m1 , m2 , m3 } , 'backward'   )
idx           = cellstr2index( { m1 , m2 , m3 } , 'random'     )
idx           = cellstr2index( { m1 , m2 , m3 } , 'random'     )

% Behavior in case of borderline inputs.
% - Passing a single string
[idx, len, g] = cellstr2index( 'foo' )
% - Passing an empty cell-array
[idx, len, g] = cellstr2index( { } )
% - Passing a cell-array of empty cell-arrays
[idx, len, g] = cellstr2index( { {} , {} } )


See also:
real2index, sortable2index

Keywords:
conversion, cell-array, cellstr, indices

Version: 0.4.6

#### 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.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo