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_cmplxvec_rank_rearrange (m01ed)

## Purpose

nag_sort_cmplxvec_rank_rearrange (m01ed) rearranges a vector of complex numbers into the order specified by a vector of ranks.

## Syntax

[cv, irank, ifail] = m01ed(cv, m1, irank, 'm2', m2)
[cv, irank, ifail] = nag_sort_cmplxvec_rank_rearrange(cv, m1, irank, 'm2', m2)

## Description

nag_sort_cmplxvec_rank_rearrange (m01ed) is designed to be used typically in conjunction with the M01D ranking functions. After one of the M01D functions has been called to determine a vector of ranks, nag_sort_cmplxvec_rank_rearrange (m01ed) can be called to rearrange a vector of complex numbers into the rank order. If the vector of ranks has been generated in some other way, then nag_sort_permute_check (m01zb) should be called to check its validity before nag_sort_cmplxvec_rank_rearrange (m01ed) is called.

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{cv}\left({\mathbf{m2}}\right)$ – complex array
Elements m1 to m2 of cv must contain complex values to be rearranged.
2:     $\mathrm{m1}$int64int32nag_int scalar
m1 and m2 must specify the range of the ranks supplied in irank and the elements of cv to be rearranged.
Constraint: $0<{\mathbf{m1}}\le {\mathbf{m2}}$.
3:     $\mathrm{irank}\left({\mathbf{m2}}\right)$int64int32nag_int array
Elements m1 to m2 of irank must contain a permutation of the integers m1 to m2, which are interpreted as a vector of ranks.

### Optional Input Parameters

1:     $\mathrm{m2}$int64int32nag_int scalar
Default: the dimension of the arrays irank, cv. (An error is raised if these dimensions are not equal.)
m1 and m2 must specify the range of the ranks supplied in irank and the elements of cv to be rearranged.
Constraint: $0<{\mathbf{m1}}\le {\mathbf{m2}}$.

### Output Parameters

1:     $\mathrm{cv}\left({\mathbf{m2}}\right)$ – complex array
These values are rearranged into rank order. For example, if ${\mathbf{irank}}\left(i\right)={\mathbf{m1}}$, then the initial value of ${\mathbf{cv}}\left(i\right)$ is moved to ${\mathbf{cv}}\left({\mathbf{m1}}\right)$.
2:     $\mathrm{irank}\left({\mathbf{m2}}\right)$int64int32nag_int array
Used as internal workspace prior to being restored and hence is unchanged.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{m2}}<1$, or ${\mathbf{m1}}<1$, or ${\mathbf{m1}}>{\mathbf{m2}}$.
${\mathbf{ifail}}=2$
Elements m1 to m2 of irank contain a value outside the range m1 to m2.
${\mathbf{ifail}}=3$
Elements m1 to m2 of irank contain a repeated value.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
If ${\mathbf{ifail}}={\mathbf{2}}$ or ${\mathbf{3}}$, elements m1 to m2 of irank do not contain a permutation of the integers m1 to m2. On exit, the contents of cv may be corrupted. To check the validity of irank without the risk of corrupting cv, use nag_sort_permute_check (m01zb).

## Accuracy

Not applicable.

The average time taken by the function is approximately proportional to $n$, where $n={\mathbf{m2}}-{\mathbf{m1}}+1$.

## Example

This example reads a matrix of complex numbers and rearranges its rows so that the elements in the $k$th column are in ascending order of modulus. To do this, the program first calls nag_sort_realvec_rank (m01da) to rank the moduli of the elements in the $k$th column, and then calls nag_sort_cmplxvec_rank_rearrange (m01ed) to rearrange each column into the order specified by the ranks. The value of $k$ is read from the datafile.
```function m01ed_example

fprintf('m01ed example results\n\n');

cm = [   6 + 1i    5 - 2i    4 + 4i;
5 - 3i    2 - 2i    1 + 1i;
2 + 2i    4 + 1i    9 - 3i;
4 + 2i    9 + 6i    6 + 4i;
4 + 0i    9 + 3i    5 + 1i;
4 - 8i    1 + 5i    2 + 1i;
3 - 3i    4 - 5i    1 + 0i;
2 + 4i    4 - 2i    6 - 1i;
1 + 1i    6 + 1i    4 + 0i;
9 + 1i    3 + 3i    2 - 4i;
6 - 1i    2 + 3i    5 - 3i;
4 - 5i    9 + 9i    6 + 7i];
m2 = size(cm,1);
n  = size(cm,2);
k  = 2;

% Moduli of column k
cmak = abs(cm(:,k));

% Get ranks from cmak
m1 = int64(1);
order = 'Ascending';
[irank, ifail] = m01da(cmak, m1, order);

% Order columns of cm by irank
for j=1:n
[cm(:,j), irank, ifail] = m01ed( ...
cm(:,j), m1, irank);
end

fprintf('Matrix sorted on column %2d\n', k);
for i = m1:m2
disp(cm(i,:));
end

```
```m01ed example results

Matrix sorted on column  2
5.0000 - 3.0000i   2.0000 - 2.0000i   1.0000 + 1.0000i

6.0000 - 1.0000i   2.0000 + 3.0000i   5.0000 - 3.0000i

2.0000 + 2.0000i   4.0000 + 1.0000i   9.0000 - 3.0000i

9.0000 + 1.0000i   3.0000 + 3.0000i   2.0000 - 4.0000i

2.0000 + 4.0000i   4.0000 - 2.0000i   6.0000 - 1.0000i

4.0000 - 8.0000i   1.0000 + 5.0000i   2.0000 + 1.0000i

6.0000 + 1.0000i   5.0000 - 2.0000i   4.0000 + 4.0000i

1.0000 + 1.0000i   6.0000 + 1.0000i   4.0000 + 0.0000i

3.0000 - 3.0000i   4.0000 - 5.0000i   1.0000 + 0.0000i

4.0000 + 0.0000i   9.0000 + 3.0000i   5.0000 + 1.0000i

4.0000 + 2.0000i   9.0000 + 6.0000i   6.0000 + 4.0000i

4.0000 - 5.0000i   9.0000 + 9.0000i   6.0000 + 7.0000i

```