The module "real2index" of the Mastrave modelling library
Copyright and license notice of the function real2index
Copyright © 2005,2006,2007,2008,2009,2010 Daniele de Rigo
The file real2index.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 ] = real2index( reals , mode = 'ascending' )
Description
Floating point values cannot be used as indices. @real2index maps floating point numbers into positive integers preserving their order. In case of sparse matrices, the mapping replaces only nonzeros. If reals is a cell-array of matrices 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) reals which may occur several times in reals . To each occurrence of the same value corresponds the same index. The set of these correspondences is returned in the vector 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').
In case some element of reals is a string, it is considered as a matrix of characters each of them is converted in its ASCII numerical code. If you need to consider strings as atomic elements to which associate single indices (instead of associating an index for each character composing a given string), you could consider to use instead @cellstr2index .
Input arguments
reals ::cellnumeric-1|cellnumeric-2:: Matrix of real numbers or cell array whose elements are matrices of real numbers. mode ::string:: Criterion for generating indices from reals . (Default: 'ascending'). Valid modes are: mode │ meaning ──────────────┼───────────────────────────────── 'ascending' │ Indices are generated by │ associating to each element of │ reals the position of its │ first occurrence in the set of │ reals elements sorted in │ ascending order. ──────────────┼───────────────────────────────── 'descending' │ Indices are generated by │ associating to each element of │ reals the position of its │ first occurrence in the set of │ reals elements sorted in │ descending order. ──────────────┼───────────────────────────────── 'forward' │ Indices are generated by │ associating to each element of │ reals the position of its │ first occurrence in the set of │ reals elements. ──────────────┼───────────────────────────────── 'backward' │ Indices are generated by │ associating to each element of │ reals the position of its │ first occurrence in the set of │ reals elements listed │ in reverse order. ──────────────┼───────────────────────────────── 'random' │ Indices are generated by │ associating to each element of │ reals the position of its │ first occurrence in the set of │ reals elements randomly │ shuffled.
Example of usage
% Basic usage (strings are considered ASCII integer matrices) m1 = '[ idx, n, gloss ] = real2index( reals )' m2 = randn(1,20) m3 = m2( ceil(rand(1,20)*20) ) m4 = zeros(3,2); m4(:) = m2(1:numel(m4)) m5 = zeros(5); m5(ceil(rand(1,numel(m4))*25)) = m4(:) m5 = sparse( m5 ) idx = real2index( m1 ) [idx, len] = real2index( {m1 , m2} ) [idx, len] = real2index( {m1 , m2 , m3} ) [idx, len] = real2index( {m1 , m2 , m4} ) [idx, len] = real2index( {m1 , m2 , m5} ) [idx, len] = real2index( {m4 , m5} ) [idx, len] = real2index( m5 ) [idx, len, g] = real2index( m2 ) m2_rep = zeros(size(m2)); m2_rep(:) = g(idx) [idx, len, g] = real2index( m1 ) m1_rep = char( zeros(size(m1)) ); m1_rep(:) = char( g(idx) ) % How mode affects the generated indices idx = real2index( m3 , 'ascending' ) idx = real2index( m3 , 'descending' ) idx = real2index( m3 , 'forward' ) idx = real2index( m3 , 'backward' ) idx = real2index( m3 , 'random' ) idx = real2index( m3 , 'random' )
See also: sortable2index, cellstr2index, cell2sparse Keywords: conversion, cell-array, real, indices Version: 0.7.2
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.