在C语言中将一个二维数组分割成多个小的二维数组的数组

4

给定:

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8

我希望将二维数组(结构体MATRIX)拆分为一组结构体MATRIX的数组,给定一个块大小CS: 假设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

这是我的矩阵结构体:
typedef struct MATRIX {
    int nrow;
    int ncol;
    int **element;
} MATRIX;

以下是将它们分开的函数:

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]);
    }


}

请注意,PRINTM基本上打印矩阵,它通过检查segs[r].nrow和ncol来知道限制,而CreateMatrix需要以下输入(&matrix、行数、列数、填充类型),并从内部进行malloc操作。
filltype: 
0- generates zeroth matrix
1- generates identity
else A[i][j] = j; for simplicity

问题在于,如果我打印矩阵Segs [i],它们都会使用CreateMatrix给定的默认值下降,而不是新添加的值。
澄清: 好的,如果你们检查一下SegmentMatrix函数中的最后一个PRINTM,它的输出就好像for循环没有发生过一样,也就是说,我可以删除for循环,得到相同的输出结果... 我在SegmentMatrix中这一行做错了什么?
Segs[r].element[i][j] = input.element[i][j];

你会在哪里调用PRINTM函数以显示错误输入?我想在你上面的代码中看到它被调用,以确保它不是一个本末倒置的问题。 - Michael Dorgan
如果您查看SegmentMatrix中的最后一条语句,您将看到PRINTM,它显示了段的默认值,好像整个for循环没有发生过。 - zellwwf
@MichaelDorgan 希望这能解释清楚。 - zellwwf
2个回答

6

我不明白为什么要用ChunkSize和未初始化的r进行乘法运算,我建议简化代码(经验法则:如果看起来很混乱,那就太复杂了)。你只需要一个三维数组来存储块的数组,并使用模数算术和整数除法将其插入到适当块的适当列中:

/* 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];
        }
    }
}

演示,又称为如何调用它:
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;
}

1
+1 我认为他应该接受你的答案。我太累了,没能检查他的代码。所以我简单地写了我的伪代码。 - qwr
谢谢大家...我会尽快查看并回复,但是结构体的作用是为了在必要时计算矩阵的大小。 - zellwwf
这是程序的一部分,应该从文件中读取矩阵,并使用MPI(MPICH2)对它们进行操作。此部分(我发布的部分)是关于它们相乘的。我已经使用上面的结构轻松完成了串行计算。Chunksize 字面上是从矩阵 A 发送到节点的列数,它在进程 0 中计算,只需传递给函数即可(我不知道为什么您认为它未初始化),而 P 是进程数。最后一个 for 循环是为了确保无论我有多少个进程,都可以正确调用这个循环。@H2CO3 - zellwwf

2
问题不太清楚,所以我认为他只是想知道如何实现这一点。因此,我写了这个简单的伪代码。否则,请接受我的道歉:
matrix[i] matrix
//matrixes total column size should be bigger big 2d array column size
first condition check: sum(matrix[i].colsize)>=big2d.colsize
//in this simple code raw sizes must be equal
second condition: for all i matrix[i].rawsize=big2d.rawsize
//if columns sizes will be equal the algorithm could be simplified , does not mean optimized
 //splitting big2d into matrixes
for (int br=0;br<big2d.rawsize;br++){
i=0;//store matrix index
int previndex=0;//store offset for next matrix
  for(int bc=0;bc<big2d.colsize;bc++){

      matrix[i].val[bc-previndex][br]=big2d.val[bc][br]; //assign (bc,br) 

      if(bc-previndex==matrix[i].colsize-1){
             i++; //move to next matrix;//if we not have next matrix then break;
            previndex=bc+1; 
          }
     /*if it be for equal chunks matrixes offset can be calculated this way too
         matrix[bc/chunk].val[bc%chunk][br]=big2d.val[bc][br];
      */
  }//loop columns
}//loop raws

1
请在您的代码中添加一些注释,明确说明它正在执行什么操作。单凭代码本身并不清晰易懂... - Michael Dorgan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接