1

为了实现 Strassen 算法,我想将一个二次矩阵拆分为 4 个二次子矩阵。

矩阵都表示为一维数组。

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 插入什么,尝试了一些组合,但它只是没有给我正确的值......

4

2 回答 2

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];
        }
    }
于 2013-10-25T20:41:07.110 回答
-1

非常感谢您发回答案。

对于所有在这里挣扎了一天的人来说,Java 中相同问题的 1D 方法。

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]);
        }
    }
}
于 2019-03-27T20:16:41.147 回答