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_permute_invert (m01za)

## Purpose

nag_sort_permute_invert (m01za) inverts a permutation, and hence converts a rank vector to an index vector, or vice versa.

## Syntax

[iperm, ifail] = m01za(iperm, m1, 'm2', m2)
[iperm, ifail] = nag_sort_permute_invert(iperm, m1, 'm2', m2)

## Description

There are two common ways of describing a permutation using an integer vector iperm. The first uses ranks: iperm(i)${\mathbf{iperm}}\left(i\right)$ holds the position to which the i$i$th data element should be moved in order to sort the data; in other words its rank in the sorted order. The second uses indices: iperm(i)${\mathbf{iperm}}\left(i\right)$ holds the current position of the data element which would occur in i$i$th position in sorted order. For example, given the values
 3.5 5.9 2.9 0.5 $3.5 5.9 2.9 0.5$
to be sorted in ascending order, the ranks would be
 3.0   4.0   2.0   1.0 $3.0 4.0 2.0 1.0$
and the indices would be
 4.0   3.0   1.0   2.0 $4.0 3.0 1.0 2.0$
The M01D functions generate ranks, and the M01E functions require ranks to be supplied to specify the reordering. However if it is desired simply to refer to the data in sorted order without actually reordering them, indices are more convenient than ranks (see the example in Section [Example]).
nag_sort_permute_invert (m01za) can be used to convert ranks to indices, or indices to ranks, as the two permutations are inverses of each another.

None.

## Parameters

### Compulsory Input Parameters

1:     iperm(m2) – int64int32nag_int array
m2, the dimension of the array, must satisfy the constraint 0 < m1m2$0<{\mathbf{m1}}\le {\mathbf{m2}}$.
Elements m1 to m2 of iperm must contain a permutation of the integers m1 to m2.
2:     m1 – int64int32nag_int scalar
m1 and m2 must specify the range of elements used in the array iperm and the range of values in the permutation, as specified under iperm.
Constraint: 0 < m1m2$0<{\mathbf{m1}}\le {\mathbf{m2}}$.

### Optional Input Parameters

1:     m2 – int64int32nag_int scalar
Default: The dimension of the array iperm.
m1 and m2 must specify the range of elements used in the array iperm and the range of values in the permutation, as specified under iperm.
Constraint: 0 < m1m2$0<{\mathbf{m1}}\le {\mathbf{m2}}$.

None.

### Output Parameters

1:     iperm(m2) – int64int32nag_int array
These elements contain the inverse permutation of the integers m1 to m2.
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 m1 < 1${\mathbf{m1}}<1$, or m1 > m2${\mathbf{m1}}>{\mathbf{m2}}$.
ifail = 2${\mathbf{ifail}}=2$
Elements m1 to m2 of iperm contain a value outside the range m1 to m2.
ifail = 3${\mathbf{ifail}}=3$
Elements m1 to m2 of iperm contain a repeated value.
If ${\mathbf{ifail}}={\mathbf{2}}$ or 3${\mathbf{3}}$, elements m1 to m2 of iperm do not contain a permutation of the integers m1 to m2; on exit these elements are usually corrupted. To check the validity of a permutation without the risk of corrupting it, use nag_sort_permute_check (m01zb).

Not applicable.

None.

## Example

```function nag_sort_permute_invert_example
iperm = [int64(11);9;3;7;6;5;4;2;1;12;10;8];
m1 = int64(1);
[ipermOut, ifail] = nag_sort_permute_invert(iperm, m1)
```
```

ipermOut =

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

ifail =

0

```
```function m01za_example
iperm = [int64(11);9;3;7;6;5;4;2;1;12;10;8];
m1 = int64(1);
[ipermOut, ifail] = m01za(iperm, m1)
```
```

ipermOut =

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

ifail =

0

```