简体   繁体   English

如何将两个不同大小的矩阵相乘

[英]How to multiply two matrices of different sizes

I'm working on a simple encryption algorithm, so I need to multiply two matrices: 我正在研究一种简单的加密算法,因此我需要将两个矩阵相乘:

This is the first: 这是第一个:

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

This is the second: 这是第二个:

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

According to this page with which I work, the result should be: 根据我处理的本页 ,结果应为:

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

Here I leave the code that does not work: 我在这里留下了无效的代码:

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("");
    }
}
}

Apparently in the count of the variables, at some point, a variable does not match the size of a matrix but I have not been able to solve it. 显然,在变量计数方面,某个变量与矩阵的大小不匹配,但我无法解决。

Here is a solution similar to what you are trying to do, you can use it if you wish. 这是一种与您尝试执行的操作类似的解决方案,您可以根据需要使用它。 Your problem stems from the fact that you are confused with your dimensions. 您的问题源于您对尺寸感到困惑的事实。

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();
    }
}

} }

In a double array, its [rows][columns] - your output will have 3 rows and 4 columns (as you can see from your website), so your dimensions for result should be resultado[3][4]. 在双数组中,其[行] [列]-您的输出将具有3行4列(如从网站上看到的),因此结果的尺寸应为resultado [3] [4]。 Your mensaje matrix should be [3][4] and not [4][3]. 您的Mensaje矩阵应为[3] [4],而不是[4] [3]。 I think you can figure out the rest. 我想您可以解决其余的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM