The unchecked module "multi2mat_" of the Mastrave modelling library


Daniele de Rigo


Copyright and license notice of the function multi2mat_



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

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



[ matrix, siz ] = multi2mat_( multi_array, dim )





Warning: multi2mat_ should not be used in end-user code because it deliberately skips input and output arguments checks and other pre- and post-condition testing. End-user code should use instead the multi2mat function (without the ending underscore).

Some functions are defined for matrices but not for multidimensional arrays (md-arrays). multi2mat_ allows to deal with md-arrays even with those ill-expandable functions which operate column by column. This is done without using explicit loops, which would be interpreted and possibly inefficient loops. Column-wise operations by default apply to the columns of a matrix as if the matrix were a vector of column vectors. This class of functions includes reductions (in the APL sense) such as sum(.), prod(.); and scans (in the APL sense) such as cumsum(.), cumprod(.). The aforementioned basic functions extend to md-arrays, however some user-defined functions may not allow the same. multi2mat_ converts a k-dimensional array multi_array of size siz :
[ n1 ... n dim ... nk ] into a matrix of size:
[ n dim prod([n1 .. n dim -1 n dim +1 nk]) ] as if multi_array were a collection of vectors having n dim elements each and being stored in the nd-array along its dim dimension.

The function mat2multi_ can reverse the conversion.

Input arguments



 multi_array       ::numcellstring,!sparse::
                   Numeric multidimensional array, vector, matrix
                   or string, or (multidimensional) cell-array of strings.

 dim               ::scalar_natural_nonzero::
                   Dimension of  multi_array  to which each  matrix 
                   column vector must belong.

Example of usage



   % Basic usage:
   mda     = zeros( 3, 4, 2);
   mda(:)  = 1:numel( mda )

   % 4x2 vectors of 3 elements each
   dim     = 1  
   multi2mat_( mda , dim )

   % 3x2 vectors of 4 elements each
   dim     = 2  
   multi2mat_( mda , dim )

   % 3x4 vectors of 2 elements each
   dim     = 3  
   multi2mat_( mda , dim )

   % Reverse the conversion:
   [ mat , siz ] = multi2mat_( mda , dim );
   mat2multi_( mat , dim , siz )

   % Scan example using cumsum:
   scan1   = cumsum( mda , dim )
   scan2   = mat2multi_( cumsum( mat , 1 ) , dim , siz )
   scan1  == scan2

   % Reduction example using sum:
   red1    = sum( mda , dim )
   red_siz = siz; red_siz( dim ) = 1
   red2    = mat2multi_( sum( mat , 1 ) , dim , red_siz )
   red1   == red2

See also:
   mat2multi, mdeal

   multidimensional-array, smart reshape, column-wise

Version: 0.2.5




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