2013-05-13 70 views
4

鉴于:分割的2D阵列,以更小的二维数组的数组用C

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

我想2D阵列(结构MATRIX)分成结构MATRIX 的阵列给出CHUNKSIZE 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和NcoI 和CreateMatrix采用以下输入的限制(&矩阵,行数,列数,填充类型)以及内部的malloc。

filltype: 
0- generates zeroth matrix 
1- generates identity 
else A[i][j] = j; for simplicity 

的问题是,如果我打印矩阵SEGS [我],他们都下来有没有新增加的值由CreateMatrix给出的默认值,和。

澄清: 好了,如果你们检查SegmentMatrix功能,去年PRINTM,它输出矩阵仿佛for循环没有发生,又名,我可以删除for循环,并会得到相同的输出。 。 难道我错了这条线(从SegmentMatrix拍摄)

Segs[r].element[i][j] = input.element[i][j]; 
+0

你打电话给PRINTM的地方是否会出现错误的输入?我希望看到在上面的代码中提到,以确保它在马类型问题之前不是购物车。 – 2013-05-13 19:03:44

+0

如果你看SegmentMatrix中的最后一个语句,你会看到PRINTM,它显示了seg的默认值,就好像整个for循环没有发生 – zellwwf 2013-05-13 21:11:03

+0

@MichaelDorgan希望解释它 – zellwwf 2013-05-13 21:22:00

回答

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

示范,一。 ķ。一个。如何打电话给我:

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 2013-05-13 19:37:10

+0

@QWR谢谢。 – 2013-05-13 19:44:06

+0

谢谢你们...我会查看它并尽快响应,但是结构在那里保持矩阵大小的计数以防万一 – zellwwf 2013-05-13 21:07:37

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

请准确地添加一些评论你的代码在做什么。它本身并不明确...... – 2013-05-13 19:01:57