nag_make_indices (m01zac) (PDF version)
m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

NAG Library Function Document

nag_make_indices (m01zac)

+ Contents

    1  Purpose
    7  Accuracy

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 <nag.h>
#include <nagm01.h>
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: ranks[i]  holds the index value to which the i+1 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: ranks[i]  holds the current index value of the data element which would occur in i+1 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.

4  References

None.

5  Arguments

1:     ranks[n]size_tInput/Output
On entry: ranks must contain a permutation of the Integers 0 to n-1 .
On exit: ranks contains the inverse permutation.
2:     nsize_tInput
On entry: the length of the array ranks.
3:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_BAD_RANK
Invalid ranks vector.
Elements of ranks contain a value outside the range 0 to n-1  or contain a repeated value. ranks does not contain a permutation of the Integers 0 to n-1 ; on exit these elements are usually corrupted.
NE_INT_ARG_GT
On entry, n=value.
Constraint: nvalue.
n is limited to an implementation-dependent size which is printed in the error message.
NE_INT_ARG_LT
On entry, n=value.
Constraint: n0.

7  Accuracy

Not applicable.

8  Parallelism and Performance

Not applicable.

9  Further Comments

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)


nag_make_indices (m01zac) (PDF version)
m01 Chapter Contents
m01 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2014