将多维数组用于矩阵乘法Java

时间:2018-11-29 20:20:49

标签: java arrays multidimensional-array matrix-multiplication

我正在尝试编写将两个给定矩阵相乘并显示结果的代码。这是到目前为止我记下来的代码:

double firstMatrix[][] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    double secondMatrix[][] = { { 1, 2, 3 }, { 4, 5, 6 } };

    double[][] thirdMatrix = multiplyMatrix((firstMatrix), (secondMatrix));

    System.out.println("The product of the matrices is ");
    for (int i = 0; i < thirdMatrix.length; i++) {
        for (int j = 0; j < thirdMatrix[0].length; j++) {
            System.out.print(firstMatrix[i][j] + " ");
            if (i == 1 && j == 2) {
                System.out.print("  *  ");
            } else {
                System.out.print("    ");
            }
        }
        for (int j = 0; j < thirdMatrix[0].length; j++) {
            System.out.print(secondMatrix[i][j] + " ");
            if (i == 1 && j == 2) {
                System.out.print(" = ");
            } else {
                System.out.print("  ");
            }
        }
        for (int j = 0; j < thirdMatrix[0].length; j++) {
            System.out.printf("%.1f", thirdMatrix[i][j]);
        }
        System.out.println();
    }

}

public static double[][] multiplyMatrix(double[][] a, double[][] b) {
    double c[][] = new double[3][3];
    for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < b.length; j++) {
            for (int k = 0; k < a.length; k++) {
                c[i][j] += ((a[i][k]) * (b[k][j]));

            }

        }
    }
    return c;}}

但是,当我尝试执行程序时,由于边界错误,我会收到一个数组索引,这种情况不应该发生,因为我的for循环确保不要超出数组范围。我不知道在哪里更改代码,因为在我尝试执行之前,它看起来还不错。任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:1)

如果第一矩阵的尺寸为mxn,第二矩阵的尺寸为nxp,则乘积的尺寸为mxp。对?
因此,三重循环的第二个层次应该迭代乘积数组的第二个维度:

for (int j = 0; j < b.length; j++)

应为:

for (int j = 0; j < b[0].length; j++)

b[0].length是矩阵b的列数。
,三重循环的3d级别应为:

for (int k = 0; k < b.length; k++)

b.length是矩阵b的行数,等于矩阵a的列数。
编辑
在通用版本中,您可以更改以下内容:

double c[][] = new double[3][3];

double c[][] = new double[a.length][b[0].length];

答案 1 :(得分:0)

public static void main(String[] args) {
    double firstMatrix[][] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
    double secondMatrix[][] = { { 1, 2, 3 }, { 4, 5, 6 } };

    double[][] thirdMatrix = multiplyMatrix((firstMatrix), (secondMatrix));

    System.out.println("The product of the matrices is ");
    for (int i = 0; i < thirdMatrix.length; i++) {
        for (int j = 0; j <firstMatrix[0].length; j++) {// check this one 
            System.out.print(firstMatrix[i][j] + " ");
            if (i == 1 && j == 2) {
                System.out.print("  *  ");
            } else {
                System.out.print("    ");
            }
        }
        for (int j = 0; j <thirdMatrix.length; j++) {

            if(i<=1){// you need are condition to stop
                System.out.print(secondMatrix[i][j] + " ");
                if (i == 1 && j == 2) {
                    System.out.print(" = ");
                } else {
                    System.out.print("  ");
                }
            }


        }
        for (int j = 0; j < thirdMatrix[0].length; j++) {
            System.out.print( thirdMatrix[i][j]);
        }
        System.out.println();
    }

}

public static double[][] multiplyMatrix(double[][] a, double[][] b) {
    double c[][] = new double[3][3];
    for (int i = 0; i < c.length; i++) {
        for (int j = 0; j < b[0].length; j++) {// Change b.length to b[0].length;
            for (int k = 0; k < a[0].length; k++) {// Change a.length to a[0].length;
                c[i][j] += ((a[i][k]) * (b[k][j]));

            }

        }
    }
    return c;
}
相关问题