rank01.gms : Rank a vector, and display the data in sorted order

Description

This example demonstrates how to get the rank of a data vecor and use that to
get the original data in sorted order.

Keywords: rank, sort


Category : GAMS Data Utilities library


Main file : rank01.gms   includes :  rank01.gms

$title Rank a vector, and display the data in sorted order (rank01,SEQ=135)

$onText
This example demonstrates how to get the rank of a data vecor and use that to
get the original data in sorted order.

Keywords: rank, sort
$offText


Set
   i 'Set on which random data are defined'   / a, b, d, c, e, f /
   k 'Ordered set for displaying sorted data' / 1*6 /;

Parameter
   x(i)   'Random data to be sorted'
   r(i)   'Rank values'
   s(k,i) 'Sorted data';

x(i) = uniform(0,1);

$libInclude rank x i r
display x;

* Generate a sorted list using the ordered set k.

* This assignment statement illustrates how the rank orders
* can be used to sort output for display in proper order.  This
* statement uses GAMS support for computed "leads" and "lags"
* on the ordered set k.  The loop is used to improve execution
* speed for larger dimensional sets:

loop(k$sameas(k,"1"),
   s(k+(r(i)-1),i) = x(i);
);

option  s:3:0:1;
display s;

Variable
   v(i)   'Random data to be sorted';

v.l(i) = uniform(0,1);

$onDotL
$libInclude rank v i r
display v.l;

option clear=s;
loop(k$sameas(k,"1"),
   s(k+(r(i)-1),i) = v(i);
);

option  s:3:0:1;
display s;