【问题标题】:Position of an element in a multidimensional array [ C ]多维数组中元素的位置 [C]
【发布时间】:2016-06-27 13:06:13
【问题描述】:

我必须编写一个函数来接收整数 X(线性化位置的值)和一个包含多维数组维度的数组,并且它必须将位置 X 的元素的坐标保存在第二个数组中在多维参考系中。例如:

X=2 和 array[]={A,B} 在此示例中,数组包含二维矩阵的维度 (A,B)。所以在二维参考系中的位置是:

知道:X=x*B+y ----> x=X/By=X%B - ---> 位置[]={x,y};

因此将 X 解码为 x 和 y 很简单,因为它是 2D 矩阵的普通情况,但我的程序必须处理 N 维矩阵(因此它必须将 X 解码为位置 x,y,.. ....,n) .

我的想法是应用我展示的算法,但即使我找不到可以处理通用 N 维矩阵的 C 代码(我也尝试编写递归函数但没有成功)。

有人能找到解决这个问题的方法吗? (提前谢谢!!!)

我是初学者!!!

【问题讨论】:

    标签: c arrays algorithm multidimensional-array position


    【解决方案1】:

    如果您有一个数组 DIM2[X,Y],其维度为 Xn 和 Yn,您也可以将其(如您所说)表示为一维数组。

    A[x,y] 然后会被映射到 DIM1[x + y * Xn]

    DIM1 必须有大小 (Xn * Yn)

    具有维度 Xn,Yn,Zn 的 3 维数组 B[] 可以以相同的方式映射:

    B[x,y,z] 将映射到 DIM1 [ x + y * Xn + z * Xn * Yn],DIM1 必须能够容纳 (Xn * Yn * Zn) 个项目,
    B[x,y,z,a] 将映射到 DIM1 [ x + y * Xn + z * Xn * Yn + a * Xn * Yn * 锌]

    等等

    对于一般的 N 维数组,最好使用递归,其中 100 维数组是 99 维数组的数组。如果所有维度都具有相同的大小,那将相对简单(编写它,我还提到递归可以很容易地展开为一个简单的 for 循环,在下面找到它)

        #include <stdio.h>
        #include <math.h>
        #include <malloc.h>
    
        #define max_depth  5    /* 5 dimensions        */
        #define size      10    /* array[10] of array  */
    
        // recursive part, do not use this one
        int _getValue( int *base, int offset, int current, int *coords) {
            if (--current)
               return _getValue (base + *coords*offset, offset/size, current, coords+1);
    
            return base[*coords];
        }
        // recursive part, do not use this one
        void _setValue( int *base, int offset, int current, int *coords, int newVal) {
            if (--current)
               _setValue (base + *coords*offset, offset/size, current, coords+1, newVal);
            base[*coords]=newVal;
        }
    
        // getValue: read item
        int getValue( int *base, int *coords) {
            int offset=pow( size, max_depth-1);   /* amount of ints to skip for first dimension */
            return (_getValue (base, offset, max_depth, coords));
        }
        // setValue: set an item
        void setValue( int *base, int *coords, int newVal) {
            int offset=pow( size, max_depth-1);
            _setValue (base, offset, max_depth, coords, newVal);
        }
    
        int main() {
            int items_needed = pow( size, max_depth);
    
            printf ("allocating room for %i items\n", items_needed);
            int *dataholder = (int *) malloc(items_needed*sizeof(int));
            if (!dataholder) {
                fprintf (stderr,"out of memory\n");
                return 1;
            }
            int coords1[5] = { 3,1,2,1,1 };    // access member [3,1,2,1,1]
            setValue(dataholder, coords1, 4711);
            int coords2[5] = { 3,1,0,4,2 };
            int x = getValue(dataholder, coords2);
    
            int coords3[5] = { 9,7,5,3,9 };
            /* or: access without recursion: */
            int i, posX = 0;                           // position of the wanted integer
            int skip = pow( size, max_depth-1);        // amount of integers to be skipped for "pick"ing array
            for (i=0;i<max_depth; i++) {
                posX += coords3[i] * skip;             // use array according to current coordinate
                skip /= size;                          // calculate next dimension's size
            }
            x = dataholder[posX];
    
            return x;
        }
    

    【讨论】:

    • 我多次想过递归,但每次我都无法写下代码......如果有人能展示解决代码,我会很高兴:)
    • 关于它的“想法”很简单:在每个“步骤”中,您只需选择正确的底层数组。假设 x 是 3,然后你选择第三个数组,然后选择 y:你选择 array[y] 直到你只剩下一个数组的地方。你终于选择了项目
    • 我更新了答案。我认为对于 C 初学者来说,第三种访问变体(使用 for 循环查找索引)更容易理解,因为涉及的指针运算要少得多
    • 对不起,我不明白代码在for循环中做了什么。我应该有一个数组来显示从 posX 开始的坐标列表。如果 posX=0 显然坐标是 [0 0 0 0 0 ... 0] 取决于我的原生多维矩阵的维度。我们认为 posX 是数组元素位置的整数值,它是我的 N 维矩阵的线性化。我们对元素本身的值不感兴趣,但我们的兴趣是将 posX 破译为我的 N 维矩阵的坐标。
    • 所以 X=x*B+y ----> 如果 X=2 和 array[]={A,B} 且 A=2 和 B=2 ----> x= 2/2 and y=2%2 ----> position[]={x,y};------->OUTPUT: position[0]=1 and position[1]=0 正如预期的那样。
    猜你喜欢
    • 2013-06-27
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2022-11-19
    相关资源
    最近更新 更多