RWalib C Array Library User Guide > Array Indexing > alibIndex1dToNd
  

alibIndex1dToNd
For an n-dimensional array, converts an array of one-dimensional indices into arrays of n-dimensional indices, i.e., convert a set of flat indices into their multidimensional equivalents. The PV-WAVE API for this routine is the INDEX_CONV Function.
Prototype
void alibIndex1dToNd( wvlong n, wvlong *d, wvlong m, wvlong *i, wvlong **j )
Parameters
n — (Input) The number of dimensions for the reference array. This array, possibly virtual, is the array to which the indices refer.
*d — (Input) The n-element array of dimensions for the reference array.
m — (Input) The number of one-dimensional indices to be converted to n-dimensional indices.
*i — (Input) The m-element array of one-dimensional indices into the reference array.
**j — (Input/Output) The n-element array of pointers to m-element arrays. On output, j contains the n-dimensional equivalents of one-dimensional indices i. For a reference array c, the relationship between one-dimensional indices i and n-dimensional indices j is:
c[ j[0][k], ..., j[n-1][k] ] = c[ i[k] ];
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
    /* array of dimensions to use for two-dimensional, three-dimensional, and four-dimensional examples */
        wvlong dims[4] = {2,3,4,5};
    /* output index array to use for two-dimensional, three-dimensional, and four-dimensional examples */
        wvlong *j[4];
    /* allocate arrays big enough for two-dimensional, three-dimensional, and four-dimensional examples */
        wvlong *i, k;
        i = (wvlong*)malloc(120*sizeof(wvlong));
        for ( k=0; k<4; k++ ) j[k] = (wvlong*)malloc(120*sizeof(wvlong));
    printf( "\n\n initialize one-dimensional indices to use in two-dimensional, three-dimensional, four-dimensional examples" );
        alibinit( NULL, NULL, NULL, NULL );
        alibIndexl( 120, 0, 1, i );
        alibPrintArrayl( 1, 1, 12, 10, i, "%8lld" );
    printf( "\n\n for (4,5) array convert all 1d indices to 2d indices\n" );
        alibIndex1dToNd( 2, &dims[2], 20, i, j );
        for ( k=0; k<20; k++ ) {
            printf( "%8lld %8lld\n", j[0][k], j[1][k] );
        }
    printf( "\n\n for (3,4,5) array convert all 1d indices to 3d indices\n" );
        alibIndex1dToNd( 3, &dims[1], 60, i, j );
        for ( k=0; k<60; k++ ) {
            printf( "%8lld %8lld %8lld\n", j[0][k], j[1][k], j[2][k] );
        }
    printf( "\n\n for (2,3,4,5) array convert 1d indices to 4d indices\n" );
        alibIndex1dToNd( 4, dims, 120, i, j );
        for ( k=0; k<120; k++ ) {
            printf( "%8lld %8lld %8lld %8lld\n",
                j[0][k], j[1][k], j[2][k], j[3][k] );
        }
}
 
Output:
 
 
 initialize one-dimensional indices to use in two-dimensional, three-dimensional, four-dimensional examples
 
 
       0       1       2       3       4       5       6       7       8       9
      10      11      12      13      14      15      16      17      18      19
      20      21      22      23      24      25      26      27      28      29
      30      31      32      33      34      35      36      37      38      39
      40      41      42      43      44      45      46      47      48      49
      50      51      52      53      54      55      56      57      58      59
      60      61      62      63      64      65      66      67      68      69
      70      71      72      73      74      75      76      77      78      79
      80      81      82      83      84      85      86      87      88      89
      90      91      92      93      94      95      96      97      98      99
     100     101     102     103     104     105     106     107     108     109
     110     111     112     113     114     115     116     117     118     119
 
 
 for (4,5) array convert all 1d indices to 2d indices
       0        0
       0        1
       0        2
       0        3
       0        4
       1        0
       1        1
       1        2
       1        3
       1        4
       2        0
       2        1
       2        2
       2        3
       2        4
       3        0
       3        1
       3        2
       3        3
       3        4
 
 
 for (3,4,5) array convert all 1d indices to 3d indices
       0        0        0
       0        0        1
       0        0        2
       0        0        3
       0        0        4
       0        1        0
       0        1        1
       0        1        2
       0        1        3
       0        1        4
       0        2        0
       0        2        1
       0        2        2
       0        2        3
       0        2        4
       0        3        0
       0        3        1
       0        3        2
       0        3        3
       0        3        4
       1        0        0
       1        0        1
       1        0        2
       1        0        3
       1        0        4
       1        1        0
       1        1        1
       1        1        2
       1        1        3
       1        1        4
       1        2        0
       1        2        1
       1        2        2
       1        2        3
       1        2        4
       1        3        0
       1        3        1
       1        3        2
       1        3        3
       1        3        4
       2        0        0
       2        0        1
       2        0        2
       2        0        3
       2        0        4
       2        1        0
       2        1        1
       2        1        2
       2        1        3
       2        1        4
       2        2        0
       2        2        1
       2        2        2
       2        2        3
       2        2        4
       2        3        0
       2        3        1
       2        3        2
       2        3        3
       2        3        4
 
 
 for (2,3,4,5) array convert 1d indices to 4d indices
       0        0        0        0
       0        0        0        1
       0        0        0        2
       0        0        0        3
       0        0        0        4
       0        0        1        0
       0        0        1        1
       0        0        1        2
       0        0        1        3
       0        0        1        4
       0        0        2        0
       0        0        2        1
       0        0        2        2
       0        0        2        3
       0        0        2        4
       0        0        3        0
       0        0        3        1
       0        0        3        2
       0        0        3        3
       0        0        3        4
       0        1        0        0
       0        1        0        1
       0        1        0        2
       0        1        0        3
       0        1        0        4
       0        1        1        0
       0        1        1        1
       0        1        1        2
       0        1        1        3
       0        1        1        4
       0        1        2        0
       0        1        2        1
       0        1        2        2
       0        1        2        3
       0        1        2        4
       0        1        3        0
       0        1        3        1
       0        1        3        2
       0        1        3        3
       0        1        3        4
       0        2        0        0
       0        2        0        1
       0        2        0        2
       0        2        0        3
       0        2        0        4
       0        2        1        0
       0        2        1        1
       0        2        1        2
       0        2        1        3
       0        2        1        4
       0        2        2        0
       0        2        2        1
       0        2        2        2
       0        2        2        3
       0        2        2        4
       0        2        3        0
       0        2        3        1
       0        2        3        2
       0        2        3        3
       0        2        3        4
       1        0        0        0
       1        0        0        1
       1        0        0        2
       1        0        0        3
       1        0        0        4
       1        0        1        0
       1        0        1        1
       1        0        1        2
       1        0        1        3
       1        0        1        4
       1        0        2        0
       1        0        2        1
       1        0        2        2
       1        0        2        3
       1        0        2        4
       1        0        3        0
       1        0        3        1
       1        0        3        2
       1        0        3        3
       1        0        3        4
       1        1        0        0
       1        1        0        1
       1        1        0        2
       1        1        0        3
       1        1        0        4
       1        1        1        0
       1        1        1        1
       1        1        1        2
       1        1        1        3
       1        1        1        4
       1        1        2        0
       1        1        2        1
       1        1        2        2
       1        1        2        3
       1        1        2        4
       1        1        3        0
       1        1        3        1
       1        1        3        2
       1        1        3        3
       1        1        3        4
       1        2        0        0
       1        2        0        1
       1        2        0        2
       1        2        0        3
       1        2        0        4
       1        2        1        0
       1        2        1        1
       1        2        1        2
       1        2        1        3
       1        2        1        4
       1        2        2        0
       1        2        2        1
       1        2        2        2
       1        2        2        3
       1        2        2        4
       1        2        3        0
       1        2        3        1
       1        2        3        2
       1        2        3        3
       1        2        3        4

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.