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_realvec_rank (m01da)

## Purpose

nag_sort_realvec_rank (m01da) ranks a vector of double numbers in ascending or descending order.

## Syntax

[irank, ifail] = m01da(rv, m1, order, 'm2', m2)
[irank, ifail] = nag_sort_realvec_rank(rv, m1, order, 'm2', m2)

## Description

nag_sort_realvec_rank (m01da) uses a variant of list-merging, as described on pages 165–166 in Knuth (1973). The function takes advantage of natural ordering in the data, and uses a simple list insertion in a preparatory pass to generate ordered lists of length at least $10$. The ranking is stable: equal elements preserve their ordering in the input data.

## References

Knuth D E (1973) The Art of Computer Programming (Volume 3) (2nd Edition) Addison–Wesley

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{rv}\left({\mathbf{m2}}\right)$ – double array
Elements m1 to m2 of rv must contain double values to be ranked.
2:     $\mathrm{m1}$int64int32nag_int scalar
The index of the first element of rv to be ranked.
Constraint: ${\mathbf{m1}}>0$.
3:     $\mathrm{order}$ – string (length ≥ 1)
If ${\mathbf{order}}=\text{'A'}$, the values will be ranked in ascending (i.e., nondecreasing) order.
If ${\mathbf{order}}=\text{'D'}$, into descending order.
Constraint: ${\mathbf{order}}=\text{'A'}$ or $\text{'D'}$.

### Optional Input Parameters

1:     $\mathrm{m2}$int64int32nag_int scalar
Default: the dimension of the array rv.
The index of the last element of rv to be ranked.
Constraint: ${\mathbf{m2}}\ge {\mathbf{m1}}$.

### Output Parameters

1:     $\mathrm{irank}\left({\mathbf{m2}}\right)$int64int32nag_int array
Elements m1 to m2 of irank contain the ranks of the corresponding elements of rv. Note that the ranks are in the range m1 to m2: thus, if ${\mathbf{rv}}\left(i\right)$ is the first element in the rank order, ${\mathbf{irank}}\left(i\right)$ is set to m1.
2:     $\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, order is not 'A' or 'D'.
${\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.

## Accuracy

Not applicable.

The average time taken by the function is approximately proportional to $n×\mathrm{log}\left(n\right)$, where $n={\mathbf{m2}}-{\mathbf{m1}}+1$.

## Example

This example reads a list of double numbers and ranks them in ascending order.
```function m01da_example

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

rv = [5.3     4.6     7.8     1.7     5.3     9.9 ...
3.2     4.3     7.8     4.5     1.2     7.6];

m1 = int64(1);
order = 'Ascending';
[irank, ifail] = m01da(rv, m1, order);

fprintf('   Data   Ranks\n\n');
for i = 1:numel(rv)
fprintf('%7.1f%7d\n',rv(i),irank(i));
end

```
```m01da example results

Data   Ranks

5.3      7
4.6      6
7.8     10
1.7      2
5.3      8
9.9     12
3.2      3
4.3      4
7.8     11
4.5      5
1.2      1
7.6      9
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.60036826,"math_prob":0.9955162,"size":2671,"snap":"2021-31-2021-39","text_gpt3_token_len":822,"char_repetition_ratio":0.095988,"word_repetition_ratio":0.07079646,"special_character_ratio":0.3114938,"punctuation_ratio":0.18053098,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9975335,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-01T15:13:02Z\",\"WARC-Record-ID\":\"<urn:uuid:c57a7c31-3b62-4883-b9c1-67a76b77d3e0>\",\"Content-Length\":\"16375\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cfeb0319-491a-43d4-a743-c42461355795>\",\"WARC-Concurrent-To\":\"<urn:uuid:400e166f-648f-4511-8c8d-8a5ed5462c32>\",\"WARC-IP-Address\":\"78.129.168.4\",\"WARC-Target-URI\":\"https://www.nag.com/numeric/mb/nagdoc_mb/manual_25_1/html/m01/m01daf.html\",\"WARC-Payload-Digest\":\"sha1:EHTXFNY45FCTWKAHIRM2COBZ5NXHKOHJ\",\"WARC-Block-Digest\":\"sha1:UY5FRNJIFHMWWDMYPTKS3E4TRFP5426A\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154214.36_warc_CC-MAIN-20210801123745-20210801153745-00210.warc.gz\"}"}