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_charvec_rank_rearrange (m01ec)

## Purpose

nag_sort_charvec_rank_rearrange (m01ec) rearranges a vector of character data into the order specified by a vector of ranks.

## Syntax

[ch, irank, ifail] = m01ec(ch, m1, irank, 'm2', m2)
[ch, irank, ifail] = nag_sort_charvec_rank_rearrange(ch, m1, irank, 'm2', m2)

## Description

nag_sort_charvec_rank_rearrange (m01ec) 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_charvec_rank_rearrange (m01ec) can be called to rearrange a vector of character data 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_charvec_rank_rearrange (m01ec) is called.

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{ch}\left({\mathbf{m2}}\right)$ – cell array of strings
Elements m1 to m2 of ch must contain character data to be rearranged.
Constraint: the length of each element of ch must not exceed $255$.
2:     $\mathrm{m1}$int64int32nag_int scalar
The range of the ranks supplied in irank and the elements of ch 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, ch. (An error is raised if these dimensions are not equal.)
The range of the ranks supplied in irank and the elements of ch to be rearranged.
Constraint: $0<{\mathbf{m1}}\le {\mathbf{m2}}$.

### Output Parameters

1:     $\mathrm{ch}\left({\mathbf{m2}}\right)$ – cell array of strings
These values are rearranged into rank order. For example, if ${\mathbf{irank}}\left(i\right)={\mathbf{m1}}$, then the initial value of ${\mathbf{ch}}\left(i\right)$ is moved to ${\mathbf{ch}}\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$
 On entry, the length of each element of ch exceeds $255$.
${\mathbf{ifail}}=3$
Elements m1 to m2 of irank contain a value outside the range m1 to m2.
${\mathbf{ifail}}=4$
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{3}}$ or ${\mathbf{4}}$, elements m1 to m2 of irank do not contain a permutation of the integers m1 to m2. On exit, the contents of ch may be corrupted. To check the validity of irank without the risk of corrupting ch, 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 file of $12$-character records, each of which contains in characters $1$ to $6$ a name of a NAG function, and in characters $7$ to $12$ an integer frequency. The program first calls nag_sort_intvec_rank (m01db) to rank the integers in descending order, and then calls nag_sort_charvec_rank_rearrange (m01ec) to rearrange the names into the order specified by the ranks.
```function m01ec_example

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

ch    = {'a02aa'; 'a02ab';  'a02ak'; 'c02al'; 'c02am'; 'c05au'; ...
'c05av'; 'c05aw';  'c05ax'; 'c05ay'; 'c05az'};

ifreq = [int64(289)  523        531       169       599      1351   ...
240       136        211       183      2181];

% Rank by decreasing frequency
m1 = int64(1);
order = 'Descending';
[irank, ifail] = m01db(ifreq, m1, order);

% Order routine names
[ch, irank, ifail] = m01ec(ch, m1, irank);

fprintf('Names in order of frequency\n\n');
for i = 1:numel(ch)
fprintf('%s\n',char(ch{i}));
end

```
```m01ec example results

Names in order of frequency

c05az
c05au
c02am
a02ak
a02ab
a02aa
c05av
c05ax
c05ay
c02al
c05aw
```