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

**Daniele de Rigo**

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

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

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

groups = mat2groups(matrix,groups_length,mode= '--none' )

#### Description

Returns a sparse matrix ` groups ` whose nonzeros elements are the elements
of the input argument

`.`

**matrix**`has an amount of columns equal to the number of elements of the vector of non-negative integers`

**groups**`. Each column of`

**groups_length**`has an amount of rows equal to the corresponding element of`

**groups**`. Depending on the value of`

**groups_length**`, the elements of each`

**mode**`column may be in the same order of the corresponding elements of`

**groups**`(default behaviour), or may be ordered in ascending or descending order.`

**matrix**

#### Input arguments

matrixMatrix of numbers.::numeric::groups_lengthVector containing the desired length of each group in which::natural::has to be splitted.matrixmodeDirection in which each column of the cell-array::string::will be sorted. Valid modes are: mode │ meaning ──────────────┼─────────────────────────────────────── 'none' │ Leave the elements ofgroupsin '--none' │ the same order of those ofgroups: (default) │ nonzeros(matrix) ==groups(:) . ──────────────┼─────────────────────────────────────── 'ascend' │ Sort in ascending order. '--ascend' │ ──────────────┼─────────────────────────────────────── 'descend' │ Sort in descending order. '--descend' │matrix

#### Example of usage

% Basic conversion n_elems = [2 0 3 4 0 0 5 3 ] data = randn( 1, sum(n_elems) ) sp_data = mat2groups( data, n_elems ); full( sp_data ) all( nonzeros(sp_data) == data(:) ) % Adding sort for no-empty elements sp_data = mat2groups( data, n_elems, '--descend' ); full( sp_data ) sp_data = mat2groups( data, n_elems, '--ascend' ); full( sp_data ) % Difference between mat2groups and score [rev_id, sorted_data, sort_id] = score( sp_data , '--ascend' ); full( sorted_data )

See also: mat2multi, multi2mat Keywords: matrices, sparse matrices Version: 0.5.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.