Given:
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
I want to split a 2d array (struct MATRIX) into an array of MATRIX structure taking CS into account: suppose cs is 2, the answer will be
Seg[0]: 1 2 1 2 1 2 Seg[1]: 3 4 3 4 3 4 .... Seg[3]: 7 8 7 8 7 8
Here is my matrix structure:
typedef struct MATRIX { int nrow; int ncol; int **element; } MATRIX;
and here is the function separating them:
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]); } }
Note that PRINTM basically prints the matrix, it knows the limitations, checking segs [r] .nrow and ncol and CreateMatrix accepts the following inputs (& matrix, number of rows, number of columns, filltype) and mallocs from the inside.
filltype: 0- generates zeroth matrix 1- generates identity else A[i][j] = j; for simplicity
The problem is that if I print the Segs [i] matrices, they are all reset with the default value specified by CreateMatrix, and not just the added values.
CLARIFICATION: Okay, so if you guys check out this last PRINTM in the SegmentMatrix function, it outputs the matrices as if the for loops were not running, aka, I can remove the for loops and get the same result. I did something wrong on this line (taken from SegmentMatrix)
Segs[r].element[i][j] = input.element[i][j];