如何将1D矩阵分成4个1D子矩阵?

时间:2013-10-25 19:51:24

标签: c arrays matrix

为了实现Strassen算法,我想将二次矩阵分成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插入什么,尝试了一些组合,但它只是没有给我正确的价值......

2 个答案:

答案 0 :(得分: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];
        }
    }

答案 1 :(得分:-1)

非常感谢您发布答案。

对于所有在这里苦苦挣扎的人来说,一维解决Java中相同问题的方法。

package hw8;

import java.io.FileNotFoundException;
import static javax.print.attribute.standard.MediaSize.Engineering.A;

public class matricies {

    public static void main (String[] args) throws FileNotFoundException {
        int[] A = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
        //1 1 1 1 
        //2 2 2 2
        //3 3 3 3
        //4 4 4 4
        int i, j;
        int dim = 4;
        int new_dim = dim / 2;
        //int A = dim * dim;
        //FILL A
        int[] a11 = new int[new_dim * new_dim];
        int[] a12 = new int[new_dim * new_dim];
        int[] a21 = new int[new_dim * new_dim];
        int[] a22 = new 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[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];
            }
        }
        for (i = 0; i < a11.length; i++) {
            System.out.println ("a11: " + a11[i]);
        }
    }
}