简体   繁体   English

Java中的矩阵相乘

[英]Multiply matrices in Java

I think I'm missing something small but I can't tell what it might be.我想我错过了一些小东西,但我不知道它可能是什么。 I'm trying to create a method that will multiply two matrices.我正在尝试创建一种将两个矩阵相乘的方法。 I'm also trying to only use 2 for loops in my method instead of 3.我也试图在我的方法中只使用 2 个 for 循环而不是 3 个。

For example, if I input例如,如果我输入

{1, 2, 3},
{2, 5, 6}

and

{4, 5},
{3, 2},
{1, 1}

I should get我应该得到

{13, 12},
{29, 26}

But I end up getting但我最终得到

{5, 12, 3},
{8, 15, 6}

Above this method, I have instance variables 'private in numRows' and 'private int numColumns' to set the dimensions of the matrix.在这个方法之上,我有实例变量'private in numRows'和'private int numColumns'来设置矩阵的维度。 I have another instance variable 'private int data[][]' that's the internal storage of the matrix elements.我有另一个实例变量'private int data[][]',它是矩阵元素的内部存储。 Then I have a Constructor for a new Matrix that automatically determines dimensions.然后我有一个自动确定尺寸的新矩阵的构造函数。

I've tried swapping i's and j's and data.length for numRows/numColumns but I'm stuck at this point.我尝试将 i's 和 j's 和 data.length 交换为 numRows/numColumns 但我被困在这一点上。 Any suggestions?有什么建议么?

public Matrix(int d[][]) {
        // d.length is the number of 1D arrays in the 2D array
        numRows = d.length;
        if (numRows == 0)
            numColumns = 0;
        else
            numColumns = d[0].length; // d[0] is the first 1D array

        // create a new matrix to hold the data
        data = new int[numRows][numColumns];

        // copy the data over
        for (int i = 0; i < numRows; i++)
            for (int j = 0; j < numColumns; j++)
                data[i][j] = d[i][j];
    }

public Matrix times(Matrix m) throws IllegalArgumentException {
        Matrix m2 = this;
        if (m.numRows != m2.numColumns)
            throw new IllegalArgumentException("Matrix information isn't correct");

        Matrix newArray = new Matrix(data);
        for (int i = 0; i < data.length; i++)
            for (int j = 0; j < data.length; j++)
                newArray.data[i][j] += m.data[i][j] * m2.data[j][i];

        return newArray;
    }

first of all, not enough information is provided in your example.首先,您的示例中没有提供足够的信息。 so I made my own example where所以我做了我自己的例子

matrix1 =矩阵1 =

1 2 3 
4 5 6

and matrix2 =和矩阵2 =

7  8
9  10
11 12

so the result =所以结果=

58   64
139  154

also made a function called printMatrix() to help debugging and printing the matrix, also this line has a great problem Matrix newArray = new Matrix(data);还做了一个 function 调用printMatrix()来帮助调试和打印矩阵,这行也有很大的问题Matrix newArray = new Matrix(data); , as data represents the first matrix, so you are making a new matrix which have the same dimensions as the first matrix which is incorrect; ,因为数据代表第一个矩阵,所以你正在制作一个与第一个矩阵具有相同维度的新矩阵,这是不正确的;

if matrix1 is of dimension 2 x 3 and matrix2 is of dimension 3 x 2 then the result matrix dimension must be of size 2 x 2 not 2 x 3 .如果 matrix1 的维度为2 x 3且 matrix2 的维度为3 x 2 ,则结果矩阵维度的大小必须为2 x 2而不是2 x 3 so that's a problem in your code, also there is no way that you can do it with only 2 for loops, you will need a third for loop to help you iterate through the whole matrix and put it in the result and this is full code:所以这是您的代码中的一个问题,也没有办法只用 2 个 for 循环来完成,您需要第三个 for 循环来帮助您遍历整个矩阵并将其放入结果中,这是完整的代码:

class Matrix: class 矩阵:

public class Matrix
{
    private int numRows;
    private int numColumns;
    private int data[][];

    public Matrix(int d[][]) {

        // d.length is the number of 1D arrays in the 2D array
        numRows = d.length;
        if (numRows == 0)
            numColumns = 0;
        else
            numColumns = d[0].length; // d[0] is the first 1D array

        // create a new matrix to hold the data
        data = new int[numRows][numColumns];

        // copy the data over
        for (int i = 0; i < numRows; i++)
            for (int j = 0; j < numColumns; j++)
                data[i][j] = d[i][j];
    }

    public Matrix times(Matrix m) throws IllegalArgumentException {
        Matrix m2 = this;
        if (m.numRows != m2.numColumns)
            throw new IllegalArgumentException("Matrix information isn't correct");

        int tempM[][] = new int[data.length][m.data[0].length];
        for (int i = 0; i < data.length; i++)       // a row
            for (int j = 0; j < m.data[0].length; j++)       // b column
                for (int k = 0; k < data[0].length; k++)      // multiplication and sum iterator
                    tempM[i][j] += m2.data[i][k] * m.data[k][j];

        return new Matrix(tempM);
    }

    public void printMatrix() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
    }
}

and here is the main I used to test the code:这是我用来测试代码的主要内容:

public class Main
{
    public static void main(String[] args) {
        int m1[][] = {{1, 2, 3}, {4, 5, 6}};
        int m2[][] = {{7, 8}, {9, 10}, {11, 12}};

        Matrix matrix1 = new Matrix(m1);
        Matrix matrix2 = new Matrix(m2);

        Matrix result = matrix1.times(matrix2);
        result.printMatrix();

    }
}

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

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