2013-05-13 73 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