PDF version (NAG web site
, 64-bit version, 64-bit version)
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
Description
There are two common ways of describing a permutation using an integer vector
iperm. The first uses ranks:
holds the position to which the
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:
holds the
current position
of the data element which would occur in
th
position in sorted order. For example, given the values
to be sorted in ascending order, the ranks would be
and the indices would be
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
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.
References
None.
Parameters
Compulsory Input Parameters
- 1:
– int64int32nag_int array
-
Elements
m1 to
m2 of
iperm must contain a permutation of the integers
m1 to
m2.
- 2:
– 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:
.
Optional Input Parameters
- 1:
– 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:
.
Output Parameters
- 1:
– int64int32nag_int array
-
These elements contain the inverse permutation of the integers
m1 to
m2.
- 2:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
On entry, | , |
or | , |
or | . |
-
-
Elements
m1 to
m2 of
iperm contain a value outside the range
m1 to
m2.
-
-
Elements
m1 to
m2 of
iperm contain a repeated value.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
If
or
, 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).
Accuracy
Not applicable.
Further Comments
None.
Example
This example reads a matrix of real numbers and prints its rows
in ascending order as ranked by
nag_sort_realmat_rank_rows (m01de).
The program first calls
nag_sort_realmat_rank_rows (m01de)
to rank the rows, and then calls
nag_sort_permute_invert (m01za) to convert the rank vector to an index vector, which is used to refer to the rows in sorted order.
Open in the MATLAB editor:
m01za_example
function m01za_example
fprintf('m01za 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);
[irank, ifail] = m01za(irank, m1);
fprintf('Matrix sorted by rows\n\n');
disp(rm(irank,:));
m01za example results
Matrix sorted by rows
1 6 4
2 4 6
2 4 9
3 4 1
4 1 2
4 9 5
4 9 6
4 9 6
5 2 1
6 2 5
6 5 4
9 3 2
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015