2013-05-13 3 views
4

작은 2D 배열의 배열을 2 차원 배열 : 나는 chunksize 영역 CS 주어진 구조체 MATRIX 의 배열로 2 차원 배열 (구조체 MATRIX)를 분할 할분할을 감안할 때 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 

: 대답은 여기에

Seg[0]: 
1 2 
1 2 
1 2 
Seg[1]: 
3 4 
3 4 
3 4 
.... 
Seg[3]: 
7 8 
7 8 
7 8 

것 CS 2, 을로 내 매트릭스 구조체되어 가정

typedef struct MATRIX { 
    int nrow; 
    int ncol; 
    int **element; 
} MATRIX; 
,369 1,363,210

여기가 seperates 그 함수이다 PRINTM 기본적 행렬을 출력하는 것이

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


} 

참고는 & (다음 입력 segs [R] .nrow하고을 NcoI 및 CreateMatrix 걸립니다 확인하여 제한을 알고 행 수, 열의 수, filltype) 및 내부에서 malloc.

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

문제는 내가 [I], 그들은 모두 기본 CreateMatrix에 의해 주어진 값, 그리고 새로 추가 된 값으로 내려와 행렬의 Segs를 인쇄하는 경우이다.

설명 : 너희들이 SegmentMatrix 함수의 마지막 PRINTM을 선택하면, 그래서 그것이 행렬 루프에 대한이 발생하지 않은 것처럼, 일명, 내가 삭제할 수를 출력 좋아 루프에 대한 동일한 출력을 얻을 것입니다. . 내가

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

어디에서 PRINTM에 전화를 걸어 잘못된 입력을 표시합니까? 나는 위의 코드에서 말 유형 문제 이전에 장바구니가 아닌지 확인하기 위해 전화를 걸 었는지보고 싶습니다. –

+0

SegmentMatrix에서 마지막 문장을 보면 PRINTM을 볼 수 있습니다. for 루프 전체가 발생하지 않는 것처럼 segs의 기본값을 보여줍니다. – zellwwf

+0

@MichaelDorgan 희망 설명 : – zellwwf

답변

5

내가 볼 수 없습니다 왜, 무엇을, 나는 코드 (엄지 손가락의 규칙을 단순화 좋을 것 (어쨌든 초기화되지) ChunkSizer에 의해 곱셈으로 조작되어 그 경우 지저분 해 보인다, 너무 복잡하다). 필요한 것은 청크의 배열을 저장하기위한 3 차원 배열이고, 모듈러 산술 플러스 정수 나눗셈 해당 청크의 해당 칼럼에 삽입 :

/* 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

+0

@QWR 고맙습니다. –

+0

감사합니다 ...내가 그것을보고 최대한 빨리 응답하지만 구조체는 경우에 따라 행렬의 크기를 계속 계산합니다. – zellwwf

2

질문 불분명합니다 (SegmentMatrix에서 가져온)이 줄 뭔가 잘못을했다. 그래서 나는 그가 이것을 성취하는 방법을 알고 싶어한다고 생각했습니다. 그래서이 간단한 의사 코드를 작성했습니다. 그렇지 않으면 동의를 내 사과 :

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

제발, 코드가하는 일. 그것은 그 자체로 명확하지 않습니다 ... –