Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中将二维数组拆分为更小的二维数组_C_Arrays_Matrix - Fatal编程技术网

在C语言中将二维数组拆分为更小的二维数组

在C语言中将二维数组拆分为更小的二维数组,c,arrays,matrix,C,Arrays,Matrix,鉴于: 我想将2d数组(struct MATRIX)拆分为struct MATRIX的数组 给定一个块大小CS: 假设cs为2, 答案是 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 这是我的矩阵结构: 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;

鉴于:

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

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8
这是我的矩阵结构:

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;
问题是,如果我打印矩阵Segs[i],它们都会使用CreateMatrix给定的默认值,而不是新添加的值

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


这个问题还不清楚。所以我想他只是想知道如何做到这一点。 所以我写了这个简单的伪代码。否则请接受我的道歉:

Segs[r].element[i][j] = input.element[i][j];
矩阵[i]矩阵
//矩阵的总列大小应大于较大的2d数组列大小
第一个条件检查:和(矩阵[i].colsize)>=big2d.colsize
//在这个简单的代码中,原始大小必须相等
第二个条件:对于所有i矩阵[i]。rawsize=big2d.rawsize
//如果列大小相等,则可以简化算法,但这并不意味着优化
//将big2d分解为矩阵

对于(int br=0;br我不明白用
ChunkSize
r
进行乘法的原因和操作方法(这是未初始化的),我建议简化代码(经验法则:如果代码看起来凌乱,就太复杂了)。您只需要一个三维数组来存储数据块数组,以及模算术加整数除法来插入到相应数据块的相应列中:

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
/*'chunks'参数的可变大小维度是w/chsz元素
*(这是块的数量)
*/
空分割(整数h,整数w,整数mat[h][w],整数chsz,整数块[][h][chsz])
{
/*检查每一行*/
对于(int i=0;i
演示,又称为“如何称呼它”:

/* 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];
        }
    }
}
intmain(intagrc,char*argv[])
{
常数大小w=8;
常数大小h=3;
常数大小c=2;
内部材料[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 }
};
整数块[w/c][h][c];
分割(h、w、mat、c、块);
对于(int i=0;i
请添加一些注释,说明您的代码到底在做什么。它本身并不清楚…您将在哪里调用PRINTM以显示错误的输入?我希望在您的上述代码中看到这一点,以确保它不是“先车后马”类型的问题。如果您查看SegmentMatrix中的最后一条语句,请将看到PRINTM,它显示segs的默认值,就好像整个for循环没有显示一样happen@MichaelDorgan希望这能解释+1我想他应该接受你的答案。我太累了,没法检查他的代码。这么简单,我写了我的伪代码谢谢大家…我会尽快查看并回复,但结构是用来计算大小的为了以防万一,这是一个程序的一部分,该程序应该从文件中读取矩阵,并使用MPI(MPICH2)对其进行操作。这一部分(我已经发布)是关于将它们相乘。我使用上面的结构轻松地完成了串行操作。Chunksize实际上是我将从矩阵A发送到节点的列数,它是在进程0中计算出来的,它只是传递给函数(我不明白为什么你认为它不存在)P是进程数。最后一个for循环是确保无论有多少进程,我都能正确调用这个循环@H2CO3
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
/* 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;
}