【问题标题】:How to multiply two matrices of different sizes如何将两个不同大小的矩阵相乘
【发布时间】:2018-11-24 00:35:24
【问题描述】:

我正在研究一个简单的加密算法,所以我需要将两个矩阵相乘:

这是第一个:

86  65  76  76
69  45  71  82
65  78  68  69

这是第二个:

13  9   3   5
2   1   4   6
4   6   2   7
8   5   4   1

根据我工作的这个page,结果应该是:

2160    1675    974 1428
1927    1502    857 1194
1825    1416    919 1338

这里我留下不起作用的代码:

public class encriptar {

public static void main(String[] args) {
    double[][] encriptador = new double[4][4];
    double[][] mensaje = new double[4][3];
    double[][] resultado = new double[4][3];

    encriptador[0][0] = 13;
    encriptador[0][1] = 9;
    encriptador[0][2] = 3;
    encriptador[0][3] = 5;
    encriptador[1][0] = 2;
    encriptador[1][1] = 1;
    encriptador[1][2] = 4;
    encriptador[1][3] = 6;
    encriptador[2][0] = 4;
    encriptador[2][1] = 6;
    encriptador[2][2] = 2;
    encriptador[2][3] = 7;
    encriptador[3][0] = 8;
    encriptador[3][1] = 5;
    encriptador[3][2] = 4;
    encriptador[3][3] = 1;

    mensaje[0][0] = 86;
    mensaje[1][0] = 65;
    mensaje[2][0] = 76;
    mensaje[3][0] = 76;
    mensaje[0][1] = 69;
    mensaje[1][1] = 45;
    mensaje[2][1] = 71;
    mensaje[3][1] = 82;
    mensaje[0][2] = 65;
    mensaje[1][2] = 78;
    mensaje[2][2] = 68;
    mensaje[3][2] = 69;

    resultado = multiplicarMatrizes(encriptador, mensaje);

    imprimirMatriz(resultado);

}

public static double[][] multiplicarMatrizes(double[][] llave, double[][] mensaje) {
    double[][] resultado = new double[llave.length][mensaje[0].length];
    if (llave.length == llave[0].length && mensaje.length == llave.length) {
        for (int k = 0; k < llave.length; k++) {
            for (int mc = 0; mc < mensaje.length; mc++) {
                for (int lf = 0; lf < mensaje.length; lf++) {
                    resultado[lf][k] += mensaje[k][mc] * llave[mc][lf];
                }
            }
        }
    }
    return resultado;
}

public static void imprimirMatriz(double[][] matriz) {
    for (int i = 0; i < matriz[0].length; i++) {
        for (int l = 0; l < matriz.length; l++) {
            System.out.print(matriz[l][i] + " ");
        }
        System.out.println("");
    }
}
}

显然在变量的计数中,在某些时候,变量与矩阵的大小不匹配,但我一直无法解决。

【问题讨论】:

标签: java for-loop multidimensional-array


【解决方案1】:

这是一个类似于您尝试做的解决方案,您可以根据需要使用它。您的问题源于您对自己的尺寸感到困惑。

public class Main {

public static void main(String[] args) {
    int r1 = 3, c1 = 4;
    int r2 = 4, c2 = 4;
    int[][] firstMatrix = { {x, x, x, x}, {x, x, x, x}, {x, x, x, x} };
    int[][] secondMatrix = { {x, x, x, x}, {x, x, x, x}, {x, x, x, x}, {x, x, x, x} };

    // Mutliplying Two matrices
    int[][] product = multiplyMatrices(firstMatrix, secondMatrix, r1, c1, c2);

    // Displaying the result
    displayProduct(product);
}

public static int[][] multiplyMatrices(int[][] firstMatrix, int[][] secondMatrix, int r1, int c1, int c2) {
    int[][] product = new int[r1][c2];
    for(int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            for (int k = 0; k < c1; k++) {
                product[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
            }
        }
    }

    return product;
}

public static void displayProduct(int[][] product) {
    System.out.println("Product of two matrices is: ");
    for(int[] row : product) {
        for (int column : row) {
            System.out.print(column + "    ");
        }
        System.out.println();
    }
}

}

在双精度数组中,它的 [rows][columns] - 您的输出将有 3 行和 4 列(您可以从您的网站上看到),因此您的结果维度应该是 resultado[3][4]。您的 mensaje 矩阵应该是 [3][4] 而不是 [4][3]。我想你可以弄清楚其余的。

【讨论】:

    猜你喜欢
    • 2022-11-12
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多