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_realmat_rank_rows (m01de)

## Purpose

nag_sort_realmat_rank_rows (m01de) ranks the rows of a matrix of double numbers in ascending or descending order.

## Syntax

[irank, ifail] = m01de(rm, m1, n1, order, 'm2', m2, 'n2', n2)
[irank, ifail] = nag_sort_realmat_rank_rows(rm, m1, n1, order, 'm2', m2, 'n2', n2)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 22: m2 was made optional

## Description

nag_sort_realmat_rank_rows (m01de) 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 ${\mathbf{n1}}+1$, and so on up to column n2.
nag_sort_realmat_rank_rows (m01de) 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 $10$. 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:     $\mathrm{rm}\left(\mathit{ldm},{\mathbf{n2}}\right)$ – double array
ldm, the first dimension of the array, must satisfy the constraint $\mathit{ldm}\ge {\mathbf{m2}}$.
Columns n1 to n2 of rows m1 to m2 of rm must contain double data to be ranked.
2:     $\mathrm{m1}$int64int32nag_int scalar
The index of the first row of rm to be ranked.
Constraint: ${\mathbf{m1}}>0$.
3:     $\mathrm{n1}$int64int32nag_int scalar
The index of the first column of rm to be used.
Constraint: ${\mathbf{n1}}>0$.
4:     $\mathrm{order}$ – string (length ≥ 1)
If ${\mathbf{order}}=\text{'A'}$, the rows will be ranked in ascending (i.e., nondecreasing) order.
If ${\mathbf{order}}=\text{'D'}$, into descending order.
Constraint: ${\mathbf{order}}=\text{'A'}$ or $\text{'D'}$.

### Optional Input Parameters

1:     $\mathrm{m2}$int64int32nag_int scalar
Default: the first dimension of the array rm.
The index of the last row of rm to be ranked.
Constraint: ${\mathbf{m2}}\ge {\mathbf{m1}}$.
2:     $\mathrm{n2}$int64int32nag_int scalar
Default: the second dimension of the array rm.
The index of the last column of rm to be used.
Constraint: ${\mathbf{n2}}\ge {\mathbf{n1}}$.

### Output Parameters

1:     $\mathrm{irank}\left({\mathbf{m2}}\right)$int64int32nag_int array
Elements m1 to m2 of irank contain the ranks of the corresponding rows of rm. Note that the ranks are in the range m1 to m2: thus, if the $i$th row of rm is the first in the rank order, ${\mathbf{irank}}\left(i\right)$ is set to m1.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{m2}}<1$, or ${\mathbf{n2}}<1$, or ${\mathbf{m1}}<1$, or ${\mathbf{m1}}>{\mathbf{m2}}$, or ${\mathbf{n1}}<1$, or ${\mathbf{n1}}>{\mathbf{n2}}$, or $\mathit{ldm}<{\mathbf{m2}}$.
${\mathbf{ifail}}=2$
 On entry, order is not 'A' or 'D'.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please contact NAG.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Not applicable.

## Further Comments

The average time taken by the function is approximately proportional to $n×\mathrm{log}\left(n\right)$, where $n={\mathbf{m2}}-{\mathbf{m1}}+1$.

## Example

This example reads a matrix of double numbers and ranks the rows in ascending order.
```function m01de_example

fprintf('m01de example results\n\n');

rm = [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 = 'Ascending';

[irank, ifail] = m01de( ...
rm, m1, n1, order);

fprintf('          Data             Ranks\n\n');
for i = 1:size(rm,1)
fprintf('%7.1f',rm(i,:));
fprintf('    %7d\n',irank(i));
end

```
```m01de example results

Data             Ranks

6.0    5.0    4.0         11
5.0    2.0    1.0          9
2.0    4.0    9.0          3
4.0    9.0    6.0          7
4.0    9.0    5.0          6
4.0    1.0    2.0          5
3.0    4.0    1.0          4
2.0    4.0    6.0          2
1.0    6.0    4.0          1
9.0    3.0    2.0         12
6.0    2.0    5.0         10
4.0    9.0    6.0          8
```

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–2015