The module "cell2sparse" of the Mastrave modelling library


Daniele de Rigo


Copyright and license notice of the function cell2sparse



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

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

Function declaration



 sparse_vals = cell2sparse( cell_vals      ,
                            cell_rows = [] ,
                            siz       = [] )




Utility to transform a numeric vector or cell-array of numeric vectors cell_vals into a sparse matrix sparse_vals . If cell_vals is a numeric vector then the corresponding returned sparse matrix has a single column whose nonzero elements are the vector elements. If cell_vals is a cell-array then the returned sparse matrix has a number of columns equal to the number of numeric vectors which compose as elements the cell-array. The row position of each nonzero element of sparse_vals is determined by the input argument cell_rows containing the row indices (subscripts) -- if omitted, the values of i-th numeric vector of cell_vals are remapped as nonzero elements of the i-th column of sparse_vals starting from the first row and occupying contiguous rows in the same column.

Input arguments



 cell_vals          ::cellnumeric,cell?same_rows::
                    Numeric vector or cell-array of numeric vectors.

 cell_rows          ::cellindex,cell?same_rows::
                    Vector of indices (or cell-array of indices vectors)
                    representing the row position of the corresponding
                    elements of  cell_vals .
                    If it is an empty array  [] , each numeric vector
                    of  cell_vals  is remapped to fill the corresponding
                    column of  sparse_vals  starting from the first row
                    and occupying contiguous rows in the same column.
                    If omitted, the default value is  [] .

 siz                ::numel,2-vector::
                    Size of the returned sparse matrix  sparse_vals .
                    If it is an empty array  [] , the size is computed
                    as the minimum one able to contain all the passed
                    If omitted, the default value is  [] .

Example of usage



   vnum      = [ 1 5 4 8 3 ]

   lens      = [ 3 0 4 3 0 0 9 0]
   N         = sum( lens )
   vcell     = mat2cell( 10*[1:N] , 1 , lens )
   cell_rows = mat2cell(    [1:N] , 1 , lens )

   cell2sparse( vnum )
   full( cell2sparse( vcell ) )

   % Passing the row position of each element
   full( cell2sparse( vcell , cell_rows ) )

   % Passing the size of  sparse_vals 
   full( cell2sparse( vcell , []        , [15,10] ) )
   full( cell2sparse( vcell , cell_rows , [25,10] ) )

See also:
   cellstr2index, get_cell_elem, mat2groups

   conversion, cell-array, sparse-matrix

Version: 0.3.2




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

This page is licensed under a Creative Commons Attribution-NoDerivs 3.0 Italy License.

This document is also part of the book:
de Rigo, D. (2012). Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling.

Valid XHTML 1.0 Transitional