m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_make_indices (m01zac)

## 1  Purpose

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

## 2  Specification

 #include #include
 void nag_make_indices (size_t ranks[], size_t n, NagError *fail)

## 3  Description

There are two common ways of describing a permutation using an Integer vector ranks. The first uses ranks: ${\mathbf{ranks}}\left[i\right]$ holds the index value to which the $\left(i+1\right)$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: ${\mathbf{ranks}}\left[i\right]$ holds the current index value of the data element which would occur in $\left(i+1\right)$th position in sorted order. For example, given the values
 $3.5 5.9 2.9 0.5$
to be sorted in ascending order, the ranks would be
 $2 3 1 0$
and the indices would be
 $3 2 0 1 .$
The m01d- functions generate ranks, and the m01e- functions require indices to be supplied to specify the re-ordering. However if it is desired simply to refer to the data in sorted order without actually re-ordering them, indices are more convenient than ranks (see Section 10). nag_make_indices (m01zac) can be used to convert ranks to indices, or indices to ranks, as the two permutations are inverses of one another.

None.

## 5  Arguments

1:    $\mathbf{ranks}\left[{\mathbf{n}}\right]$size_tInput/Output
On entry: ranks must contain a permutation of the Integers 0 to ${\mathbf{n}}-1$.
On exit: ranks contains the inverse permutation.
2:    $\mathbf{n}$size_tInput
On entry: the length of the array ranks.
3:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

Invalid ranks vector.
Elements of ranks contain a value outside the range 0 to ${\mathbf{n}}-1$ or contain a repeated value. ranks does not contain a permutation of the Integers 0 to ${\mathbf{n}}-1$; on exit these elements are usually corrupted.
NE_INT_ARG_GT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\le 〈\mathit{\text{value}}〉$.
n is limited to an implementation-dependent size which is printed in the error message.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 0$.

Not applicable.

Not applicable.

None.

## 10  Example

The example program reads a matrix of real numbers and prints its rows with the elements of the 1st column in ascending order as ranked by nag_rank_sort (m01dsc). The program first calls nag_rank_sort (m01dsc) to rank the rows, and then calls nag_make_indices (m01zac) to convert the rank vector to an index vector, which is used to refer to the rows in sorted order.

### 10.1  Program Text

Program Text (m01zace.c)

### 10.2  Program Data

Program Data (m01zace.d)

### 10.3  Program Results

Program Results (m01zace.r)