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

**Daniele de Rigo**

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

Copyright © 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014 Daniele de Rigo

The file sortable2index.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,freqs] = sortable2index(sortable,mode= 'ascending' )

#### Description

Floating point values and cells of strings can be sorted but not used as
indices.
@sortable2index remaps (total-ordered) sortable collections into positive
integers preserving their order.
In case of sparse matrices, the mapping replaces only nonzeros.
Each index of ` indices ` represents an element of

`which may occur several times in`

**sortable**`. To each occurrence of the same value corresponds the same index -- as a consequence total ordering in sorting elements is required to preserve bijectivity between values and indices (e.g. despite ::sortable::, complex numbers are not ::tsortable:: allowing only partial ordering and therefore they cannot be indexed by this function). The set of these correspondences is returned in the vector or cell of strings`

**sortable**`, whose length is the`

**glossary**`of the indices set. The number of occurrences of`

**cardinality**`elements which correspond to each index is returned by`

**sortable**`. The particular way in which indices are generates can be governed using the optional`

**freqs**`input argument (if omitted, its default value is 'ascending').`

**mode**If ` sortable ` elements are all (cell-arrays of) strings or of real numbers,
you may consider to use instead of this function its specialized versions
which respectively are @cellstr2index and @real2index .

#### Input arguments

sortableCell array of strings or numerical matrix.::tsortable::modeCriterion for generating indices from::string::. (Default: 'ascending'). Valid modes are: mode │ meaning ───────────────┼───────────────────────────────── 'ascending' │ Indices are generated by '--ascending' │ associating to each element of │sortablethe position of its │ first occurrence in the set of │sortableelements sorted in │ ascending order. ───────────────┼───────────────────────────────── 'descending' │ Indices are generated by '--descending' │ associating to each element of │sortablethe position of its │ first occurrence in the set of │sortableelements sorted in │ descending order. ───────────────┼───────────────────────────────── 'forward' │ Indices are generated by '--forward' │ associating to each element of │sortablethe position of its │ first occurrence in the set of │sortableelements. ───────────────┼───────────────────────────────── 'backward' │ Indices are generated by '--backward' │ associating to each element of │sortablethe position of its │ first occurrence in the set of │sortableelements listed │ in reverse order. ───────────────┼───────────────────────────────── 'random' │ Indices are generated by '--random' │ associating to each element of │sortablethe position of its │ first occurrence in the set of │sortableelements randomly │ shuffled.sortable

#### Example of usage

% Behavior with different classes ofm{1} = { 'foobar' , 'bar' , '' , 'quux' , 'foo' , 'bar' }; m{2} = { 'foobar' }; m{3} = 'foobar'; m{4} = ceil( randn(1,15)*5 )/2; m{4}([2 5]) = nan; m{4}([4 8]) = inf; m{4}([10]) = 0; nulls_id = find( 0 == m{4} ) rows = ceil(rand(1,15)*1000) cols = ceil(rand(1,15)*10) vals = m{4}; vals(nulls_id)=-1 m{5} = sparse( rows, cols, vals, 1100, 20 ); m{5}(10)=0; m{6} = { }; m{7} = ''; m{8} = []; m{9} = sparse( 0 , 0 ); m{10} = sparse( 1100 , 15 ) for i=1:numel(m) fprintf( 1 , '\n\nm{%d} class is %s:\n', i , class(m{i}) ); disp( m{i} ); [idx, len, g] = sortable2index( m{i} ) end % Howsortableaffects the generated indices idx = sortable2index( m{4} , 'ascending' ) idx = sortable2index( m{4} , 'descending' ) idx = sortable2index( m{4} , 'forward' ) idx = sortable2index( m{4} , 'backward' ) idx = sortable2index( m{4} , 'random' ) idx = sortable2index( m{4} , 'random' )mode

See also: real2index, cellstr2index, cell2sparse Keywords: conversion, cell-array, sortable, indices Version: 0.4.1

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