3

私は次の(不完全な)機能を持っています:

/* Populates char* name with the named location of the ith (flat) element
 * of an array with ndim dimensions where the length of each dimension 
 * is already stored in the int* dim.
 * 
 * name: a pointer to where the name should be populated
 * n: the base name of the array
 * dim: an int[] containing the length of each dimension
 * ndim: length of the dim array
 * i: name of the iteration variable being used
 **/
void populateName(char *name, const char *n, int *dim, int ndim, const char *i) {
  strcpy(name, n);
  char *loc = (char*)(name + strlen(n));
  char *curr;
  for (int k = 0; k < ndim; k++) {
    ...
    sprintf(loc, "[%s]", curr);
    loc += strlen(loc);
  }
}

forループの「...」には何を入れるべきですか? たとえば、次のように populateName() を呼び出します。

int dim[2] = {3, 4};
char name[1024];
populateName(name, "x", dim, 2, "i");

次のような結果になるはずです。

name = "x[i / 3][i % 4]"

または、次のように定義された配列内の i 番目の場所にアクセスするためのその他の有効な名前:

int x[3][4];

コンテキスト: IDL で記述されたユーザー定義のデータ型とルールに基づいて大量のデータをフィルター処理する C プログラムを生成する C プログラムを作成しています。

編集:配列内の場所/座標を含むタプルを返すpython関数は、正しい方向に進む可能性があります。特に、次の配列では、各要素が配列内のフラットな位置に対応している必要があります (ここでは pylab を使用)。

In [14]: x
Out[14]: 
array([[[ 0,  1,  2],
    [ 3,  4,  5]],

   [[ 6,  7,  8],
    [ 9, 10, 11]],

   [[12, 13, 14],
    [15, 16, 17]]])

In [15]: x.flat.copy()
Out[15]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17])
4

1 に答える 1