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

Daniele de Rigo

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

[score_idx, sorted_X, sorted_idx] = score( X               ,
dim  = 1        ,
mode = 'ascend' )



#### Description

Utility for specializing

[sorted_X, sorted_idx] = sort( X , dim , mode )

score(X) returns as first element score_idx a vector representing the ranking of the elements of X when they are considered as scores. Whether X scores are to be ranked in ascending (default) or descending order, is decided with the input argument mode . If X is a vector vec , the relation between score_idx and sorted_idx is:

[score_idx, sorted_vec, sorted_idx] = score( vec );
vec( sorted_idx ) == sorted_vec
sorted_vec( score_idx ) == vec

If X is a matrix M the relation between score_idx and sorted_idx is:

siz = size(M);
[r, c] = ind2sub( siz, 1:numel(M) );
[score_idx, sorted_M, sorted_idx] = score(M);
M( sub2ind( siz, sorted_idx, c ) ) == sorted_M
sorted_M( sub2ind( siz, score_idx, c ) ) == M

If X is a sparse matrix sM , score_idx and sorted_idx are related to the nonzeros elements of sM :
[score_idx, sorted_sM, sorted_idx] = score(sM);
[r1, c1, nz] = find(sM);
[r2, c2, snz] = find(sorted_sM);
sid = nonzeros(sorted_idx);
scr = nonzeros(score_idx );
sorted_sM == sparse( r2, c2, nz(sid) )

#### Input arguments


X                 ::sortable::
Numeric vector o matrix, or string, or cell-array
of strings.

dim               ::scalar_natural_nonzero::
Dimension along which to sort  X .
If omitted, default is 1.

mode              ::string::
Direction in which  X  will be sorted.  If omitted,
default is 'ascend'.  Valid modes are:

mode     │      meaning
──────────────┼────────────────────────────
'ascend'    │ Sort in  ascending order.
'--ascend'  │
──────────────┼────────────────────────────
'descend'   │ Sort in descending order.
'--descend' │



#### Example of usage


% Basic usage.
% Passing a column vector:
A = zeros(5,1); A(:) = randperm(numel(A))*10
[revid, sA, sid]   = score( A )
A2    =  A;
A2(:) = sA( revid );
all( A(:) == A2(:) )

% Passing a row vector:
A = zeros(1,5); A(:) = randperm(numel(A))*10
[revid, sA, sid]   = score( A )
A2    =  A;
A2(:) = sA( revid );
all( A(:) == A2(:) )

% Passing a matrix:
A = zeros(3,4); A(:) = randperm(numel(A))*10

[revid, sA, sid]     = score( A )
[r, c]               = ind2sub( size(A), 1:numel(A) );
A2    =  A;
A2(:) = sA( sub2ind( size(A), revid(:), c(:) ) );
all( A(:) == A2(:) )

% Using the  dim  argument:
[revid, sA, sid]     = score( A , 2 )
[r, c]               = ind2sub( size(A), 1:numel(A) );
A2    =  A;
A2(:) = sA( sub2ind( size(A), r(:), revid(:) ) );
all( A(:) == A2(:) )

% Passing a 3-dimensional array:
A = zeros(3,4,2); A(:) = ceil(rand(1,numel(A))*numel(A))*10
[revid, sA, sid]       = score( A , 2 )
[rr{1:3}]              = ind2sub( size(A), 1:numel(A) );
A2    =  A;
A2(:) = sA( sub2ind( size(A), rr{1}(:), revid(:), rr{3}(:) ) );
all( A(:) == A2(:) )

% Passing a multidimensional cell array
cA = mat2cell( sprintf('%03d',A(:)), 1, ones(1,numel(A))*3 );
cA = reshape( cA, size(A) )
[revcid, scA, scid]    = score( cA , 2 )
all( revcid(:) == revid(:) )
all(   scid(:) ==   sid(:) )
disp('  A   cA  |  sA  scA');
for i=1:numel(A)
fprintf( 1, '%3d ''%s'' | %3d ''%s''\n', A(i), cA{i}, sA(i), scA{i} );
end


See also:
tf_score

Keywords:
ranking

Version: 0.9.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.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo