2013-10-25 197 views
1

爲了實現Strassen算法,我想將一個二次矩陣分成4個二次子矩陣。如何將1D矩陣拆分爲4個1D子矩陣?

矩陣都表示爲一維數組。

int i, j; 
int dim = 4; 
int new_dim = dim/2; 

int *A = malloc(sizeof(int) * dim * dim); 
//FILL A 
int *a11 = malloc(sizeof(int) * new_dim * new_dim); 
int *a12 = malloc(sizeof(int) * new_dim * new_dim); 
int *a21 = malloc(sizeof(int) * new_dim * new_dim); 
int *a22 = malloc(sizeof(int) * new_dim * new_dim); 
for (i = 0; i < new_dim; i++) { 
    for (j = 0; j < new_dim; j++) { 
     a11[i * new_dim + j] = A[XXXXX]; 
     a12[i * new_dim + j] = A[XXXXX]; 
     a21[i * new_dim + j] = A[XXXXX]; 
     a22[i * new_dim + j] = A[XXXXX]; 
    } 
} 

我真的不得到什麼插入爲XXXXX,嘗試了一些組合,但它只是不給我正確的價值觀......

回答

1

現在明白了...

for (i = 0; i < new_dim; i++) { 
     for (j = 0; j < new_dim; j++) { 
      a11[i * new_dim + j] = A[i*dim+j]; 
      a12[i * new_dim + j] = A[i*dim + (j+new_dim)]; 
      a21[i * new_dim + j] = A[(i+new_dim)*dim+j]; 
      a22[i * new_dim + j] = A[(i+new_dim)*dim+j+new_dim]; 
     } 
    }