File: Computing-the-rank.html

package info (click to toggle)
gsl-ref-html 2.3-1
• area: non-free
• in suites: bullseye, buster, sid
• size: 6,876 kB
• ctags: 4,574
• sloc: makefile: 35
 file content (119 lines) | stat: -rw-r--r-- 4,761 bytes parent folder | download
 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119` `````` GNU Scientific Library – Reference Manual: Computing the rank

12.4 Computing the rank

The rank of an element is its order in the sorted data. The rank is the inverse of the index permutation, p. It can be computed using the following algorithm,

for (i = 0; i < p->size; i++)  {     size_t pi = p->data[i];     rank->data[pi] = i; }

This can be computed directly from the function gsl_permutation_inverse(rank,p).

The following function will print the rank of each element of the vector v,

void print_rank (gsl_vector * v) {   size_t i;   size_t n = v->size;   gsl_permutation * perm = gsl_permutation_alloc(n);   gsl_permutation * rank = gsl_permutation_alloc(n);    gsl_sort_vector_index (perm, v);   gsl_permutation_inverse (rank, perm);    for (i = 0; i < n; i++)    {     double vi = gsl_vector_get(v, i);     printf ("element = %d, value = %g, rank = %d\n",              i, vi, rank->data[i]);    }    gsl_permutation_free (perm);   gsl_permutation_free (rank); }
``````