hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_sort_intmat_rank_rows (m01df)

Purpose

nag_sort_intmat_rank_rows (m01df) ranks the rows of a matrix of integer numbers in ascending or descending order.

Syntax

[irank, ifail] = m01df(im, m1, n1, order, 'm2', m2, 'n2', n2)
[irank, ifail] = nag_sort_intmat_rank_rows(im, m1, n1, order, 'm2', m2, 'n2', n2)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: m2 has been made optional
.

Description

nag_sort_intmat_rank_rows (m01df) ranks rows m1 to m2 of a matrix, using the data in columns n1 to n2 of those rows. The ordering is determined by first ranking the data in column n1, then ranking any tied rows according to the data in column n1 + 1n1+1, and so on up to column n2.
nag_sort_intmat_rank_rows (m01df) uses a variant of list-merging, as described on pages 165–166 in Knuth (1973). The function takes advantage of natural ordering in the data, and uses a simple list insertion in a preparatory pass to generate ordered lists of length at least 1010. The ranking is stable: equal rows preserve their ordering in the input data.

References

Knuth D E (1973) The Art of Computer Programming (Volume 3) (2nd Edition) Addison–Wesley

Parameters

Compulsory Input Parameters

1:     im(ldm,n2) – int64int32nag_int array
ldm, the first dimension of the array, must satisfy the constraint ldmm2ldmm2.
Columns n1 to n2 of rows m1 to m2 of im must contain integer data to be ranked.
2:     m1 – int64int32nag_int scalar
The index of the first row of im to be ranked.
Constraint: m1 > 0m1>0.
3:     n1 – int64int32nag_int scalar
The index of the first column of im to be used.
Constraint: n1 > 0n1>0.
4:     order – string (length ≥ 1)
If order = 'A'order='A', the rows will be ranked in ascending (i.e., nondecreasing) order.
If order = 'D'order='D', into descending order.
Constraint: order = 'A'order='A' or 'D''D'.

Optional Input Parameters

1:     m2 – int64int32nag_int scalar
Default: The first dimension of the array im.
The index of the last row of im to be ranked.
Constraint: m2m1m2m1.
2:     n2 – int64int32nag_int scalar
Default: The second dimension of the array im.
The index of the last column of im to be used.
Constraint: n2n1n2n1.

Input Parameters Omitted from the MATLAB Interface

ldm

Output Parameters

1:     irank(m2) – int64int32nag_int array
Elements m1 to m2 of irank contain the ranks of the corresponding rows of im. Note that the ranks are in the range m1 to m2: thus, if the iith row of im is the first in the rank order, irank(i)iranki is set to m1.
2:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Errors or warnings detected by the function:
  ifail = 1ifail=1
On entry,m2 < 1m2<1,
orn2 < 1n2<1,
orm1 < 1m1<1,
orm1 > m2m1>m2,
orn1 < 1n1<1,
orn1 > n2n1>n2,
orldm < m2ldm<m2.
  ifail = 2ifail=2
On entry,order is not 'A' or 'D'.

Accuracy

Not applicable.

Further Comments

The average time taken by the function is approximately proportional to n × log(n)n×log(n), where n = m2m1 + 1n=m2-m1+1.

Example

function nag_sort_intmat_rank_rows_example
im = [int64(6),5,4; ...
             5,2,1; ...
             2,4,9; ...
             4,9,6; ...
             4,9,5; ...
             4,1,2; ...
             3,4,1; ...
             2,4,6; ...
             1,6,4; ...
             9,3,2; ...
             6,2,5; ...
             4,9,6];
m1 = int64(1);
n1 = int64(1);
order = 'Descending';
[irank, ifail] = nag_sort_intmat_rank_rows(im, m1, n1, order)
 

irank =

                    2
                    4
                   10
                    5
                    7
                    8
                    9
                   11
                   12
                    1
                    3
                    6


ifail =

                    0


function m01df_example
im = [int64(6),5,4; ...
             5,2,1; ...
             2,4,9; ...
             4,9,6; ...
             4,9,5; ...
             4,1,2; ...
             3,4,1; ...
             2,4,6; ...
             1,6,4; ...
             9,3,2; ...
             6,2,5; ...
             4,9,6];
m1 = int64(1);
n1 = int64(1);
order = 'Descending';
[irank, ifail] = m01df(im, m1, n1, order)
 

irank =

                    2
                    4
                   10
                    5
                    7
                    8
                    9
                   11
                   12
                    1
                    3
                    6


ifail =

                    0



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013