将2D数组拆分为C中较小的2D数组的数组

zel*_*wwf 4 c arrays matrix

鉴于:

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8
Run Code Online (Sandbox Code Playgroud)

给定一个chunksize CS,我想将2d数组(struct MATRIX)拆分为struct MATRIX数组:假设cs为2,答案是

Seg[0]:
1 2 
1 2 
1 2
Seg[1]:
3 4 
3 4 
3 4
....
Seg[3]:
7 8
7 8
7 8
Run Code Online (Sandbox Code Playgroud)

这是我的Matrix Struct:

typedef struct MATRIX {
    int nrow;
    int ncol;
    int **element;
} MATRIX;
Run Code Online (Sandbox Code Playgroud)

这是分离它们的功能:

void SegmentMatrix(MATRIX input,MATRIX* segs,int Chunksize, int p) {
    int i,j,r;

    //Allocate segs
    for (i = 0; i<p;i++)
    {
        CreateMatrix(&(segs[i]),input.nrow ,Chunksize,0);
    }

    //Now Copy the elements from input to the segs
    //where seg0 takes from 0 to cs cols of a, and all their rows, and seg1 takes from cs to 2cs ...
    printf("Stats:\n\t P: %d\t CS: %d\n",p,Chunksize);
    for (r = 0; r<p; r++) {
        for (i = 0; i<input.nrow;i++) {
            for (j = r*Chunksize; j<r*Chunksize+Chunksize-1; j++) {
                 //I tried (&(segs[r]))->element... Doesn't work, produces wrong data
                 segs[r].element[i][j] = input.element[i][j];

        }
    }
    PRINTM(segs[r]);
    }


}
Run Code Online (Sandbox Code Playgroud)

请注意,PRINTM基本上打印矩阵,它通过检查segs [r] .nrow和ncol知道限制,并且CreateMatrix从内部获取以下输入(&矩阵,行数,列数,填充类型)和malloc.

filltype: 
0- generates zeroth matrix
1- generates identity
else A[i][j] = j; for simplicity
Run Code Online (Sandbox Code Playgroud)

问题是if if我打印矩阵Segs [i],它们都是由CreateMatrix给出的默认值,而不是新添加的值.

澄清:好的,所以,如果你们检查SegmentMatrix函数中的最后一个PRINTM,它会输出矩阵,好像for循环没有发生,也就是说,我可以删除for循环并获得相同的输出..我做了什么这行错了(取自SegmentMatrix)

Segs[r].element[i][j] = input.element[i][j];
Run Code Online (Sandbox Code Playgroud)

小智 5

我不明白为什么,你是用乘法操作什么ChunkSizer(这是无论如何未初始化),我建议简化了代码(经验法则:如果它看起来凌乱,这太复杂).您所需要的只是一个用于存储块数组的三维数组,以及模数运算和整数除法,以插入相应块的相应列:

/* the variable-sized dimension of the `chunks' argument is w / chsz elements big
 * (it's the number of chunks)
 */
void split(int h, int w, int mat[h][w], int chsz, int chunks[][h][chsz])
{
    /* go through each row */
    for (int i = 0; i < h; i++) {
        /* and in each row, go through each column */
        for (int j = 0; j < w; j++) {
            /* and for each column, find which chunk it goes in
             * (that's j / chsz), and put it into the proper row
             * (which is j % chsz)
             */
            chunks[j / chsz][i][j % chsz] = mat[i][j];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

演示,又称如何调用它:

int main(int agrc, char *argv[])
{
    const size_t w = 8;
    const size_t h = 3;
    const size_t c = 2;

    int mat[h][w] = {
        { 1, 2, 3, 4, 5, 6, 7, 8 },
        { 1, 2, 3, 4, 5, 6, 7, 8 },
        { 1, 2, 3, 4, 5, 6, 7, 8 }
    };

    int chunks[w / c][h][c];

    split(h, w, mat, c, chunks);

    for (int i = 0; i < w / c; i++) {
        for (int j = 0; j < h; j++) {
            for (int k = 0; k < c; k++) {
                printf("%3d ", chunks[i][j][k]);
            }
            printf("\n");
        }
        printf("\n\n");
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)