Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_sort_intmat_rank_columns (m01dk)

## Purpose

nag_sort_intmat_rank_columns (m01dk) ranks the columns of a matrix of integer numbers in ascending or descending order.

## Syntax

[irank, ifail] = m01dk(im, m1, n1, order, 'm2', m2, 'n2', n2)
[irank, ifail] = nag_sort_intmat_rank_columns(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_columns (m01dk) ranks columns n1 to n2 of a matrix, using the data in rows m1 to m2 of those columns. The ordering is determined by first ranking the data in row m1, then ranking any tied columns according to the data in row m1 + 1${\mathbf{m1}}+1$, and so on up to row m2.
nag_sort_intmat_rank_columns (m01dk) 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$10$. The ranking is stable: equal columns 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 ldmm2$\mathit{ldm}\ge {\mathbf{m2}}$.
Rows m1 to m2 of columns n1 to n2 of im must contain integer data to be ranked.
2:     m1 – int64int32nag_int scalar
The index of the first row of im to be used.
Constraint: m1 > 0${\mathbf{m1}}>0$.
3:     n1 – int64int32nag_int scalar
The index of the first column of im to be ranked.
Constraint: n1 > 0${\mathbf{n1}}>0$.
4:     order – string (length ≥ 1)
If order = 'A'${\mathbf{order}}=\text{'A'}$, the columns will be ranked in ascending (i.e., nondecreasing) order.
If order = 'D'${\mathbf{order}}=\text{'D'}$, into descending order.
Constraint: order = 'A'${\mathbf{order}}=\text{'A'}$ or 'D'$\text{'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 used.
Constraint: m2m1${\mathbf{m2}}\ge {\mathbf{m1}}$.
2:     n2 – int64int32nag_int scalar
Default: The second dimension of the array im.
The index of the last column of im to be ranked.
Constraint: n2n1${\mathbf{n2}}\ge {\mathbf{n1}}$.

ldm

### Output Parameters

1:     irank(n2) – int64int32nag_int array
Elements n1 to n2 of irank contain the ranks of the corresponding columns of im. Note that the ranks are in the range n1 to n2: thus, if the i$i$th column of im is the first in the rank order, irank(i)${\mathbf{irank}}\left(i\right)$ is set to n1.
2:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, m2 < 1${\mathbf{m2}}<1$, or n2 < 1${\mathbf{n2}}<1$, or m1 < 1${\mathbf{m1}}<1$, or m1 > m2${\mathbf{m1}}>{\mathbf{m2}}$, or n1 < 1${\mathbf{n1}}<1$, or n1 > n2${\mathbf{n1}}>{\mathbf{n2}}$, or ldm < m2$\mathit{ldm}<{\mathbf{m2}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, order is not 'A' or 'D'.

## Accuracy

Not applicable.

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

## Example

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

irank =

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

ifail =

0

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

irank =

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

ifail =

0

```