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];
你打電話給PRINTM的地方是否會出現錯誤的輸入?我希望看到在上面的代碼中提到,以確保它在馬類型問題之前不是購物車。 – 2013-05-13 19:03:44
如果你看SegmentMatrix中的最後一個語句,你會看到PRINTM,它顯示了seg的默認值,就好像整個for循環沒有發生 – zellwwf 2013-05-13 21:11:03
@MichaelDorgan希望解釋它 – zellwwf 2013-05-13 21:22:00