简体   繁体   中英

Trying to multiply 2d arrays (like in matrix multiplication), but only works for square matrices

I am a complete Java newbie and I was trying to multiply two 2D arrays like you would multiply two matrices. The program below only works for square matrices, but not for others. I cannot seem to figure out where I am going wrong. If someone could help me out, it would be great.

import java.util.Scanner;

public class TwoDMatrix {
    public static void main (String [] args){
        Scanner scanme = new Scanner(System.in);

        //Input dimensions of Matrix A
        System.out.println("Enter the dimensions (row x column) of Matrix A");
        int rowA = scanme.nextInt();
        int columnA = scanme.nextInt();
        int [][] matA = new int [rowA][columnA];



        //Input dimensions of Matrix B
        System.out.println("Enter the dimensions (row x column) of Matrix B");
        int rowB = scanme.nextInt();
        int columnB = scanme.nextInt();
        int [][] matB = new int [rowB][columnB];



        // Declaring new variables
        int [][] product = new int [columnA][rowB];
        int rowCountA, columnCountA, rowCountB, columnCountB;
        int rowCountProduct, columnCountProduct;
        int sum;
        String divider = "---------";



        // Input values of Matrix A
        for (rowCountA = 0; rowCountA < rowA; rowCountA++){
            for (columnCountA = 0; columnCountA < columnA; columnCountA++){
                System.out.printf("%s%d%s%d%s", "Enter the value at A(", rowCountA, ",", columnCountA, ")");
                matA[rowCountA][columnCountA] = scanme.nextInt();
            }
        }



        // Input values of Matrix B
        for (rowCountB = 0; rowCountB < rowB; rowCountB++){
            for (columnCountB = 0; columnCountB < columnB; columnCountB++){
                System.out.printf("%s%d%s%d%s", "Enter the value at B(", rowCountB, ",", columnCountB, ")");
                matB[rowCountB][columnCountB] = scanme.nextInt();
            }
        }



        //Calculate product of the two matrices
        for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){
            for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){
                sum = 0;
                for (columnCountA=0, rowCountB=0; columnCountA<columnA && rowCountB<rowB; columnCountA++, rowCountB++){
                    sum += (matA[rowCountProduct][columnCountA] * matB[rowCountB][columnCountProduct]);
                }
                product[rowCountProduct][columnCountProduct] = sum;
            }
        }



        //Prints the input matrix A
        System.out.printf("%n%s%n%s%n", "Matrix A:", divider);
        for (rowCountA = 0; rowCountA < rowA; rowCountA++){
            for (columnCountA = 0; columnCountA < columnA; columnCountA++){
                System.out.printf("%5d", matA[rowCountA][columnCountA]);
            }
            System.out.println();
        }



        //Prints the input matrix B
        System.out.printf("%n%s%n%s%n", "Matrix B:", divider);
        for (rowCountB = 0; rowCountB< rowB; rowCountB++){
            for (columnCountB = 0; columnCountB < columnB; columnCountB++){
                System.out.printf("%5d", matB[rowCountB][columnCountB]);
            }
            System.out.println();
        }



        //Prints the product
        System.out.printf("%n%s%n%s%n", "Product", divider);
        for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){
            for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){
                System.out.printf("%5d", product[rowCountProduct][columnCountProduct]);
            }
            System.out.println();
        }
    }
}

It has been a while since I learned linear algebra, but I think when you multiply a matrix A[n1][m1] by a matrix B[n2][m2], m1 must be equal to n2 and the result should be a matrix C[n1][m2].

Therefore

int [][] product = new int [columnA][rowB];

should be

int [][] product = new int [rowA][columnB];

And you should verify that columnA == rowB before you start the multiplication.

You have quite a complicated condition here :

for (columnCountA=0, rowCountB=0; columnCountA<columnA && rowCountB<rowB; columnCountA++, rowCountB++){
    sum += (matA[rowCountProduct][columnCountA] * matB[rowCountB][columnCountProduct]);
}

Remember the mathematic formula :

be A a n x l matrix, B a l x m matrix, then
forall (i,j) in [1,n]x[1,m], (AB)(i,j) = sum_(k in [1,l]) { A(i,k).B(k,j) }

Therefore, a pseudo-code for this is :

for (int i=0 ; i<A.length ; i++) {
    for (int j=0 ; j<B[0].length ; j++) {
        prod[i][j] = 0;
        for (int k=0 ; k<A[0].length ; k++) {
            prod[i][j] += A[i][k]*B[k][j];
        }
    }
}

Basically, you define your product matrix as:

    int [][] product = new int [columnA][rowB];

This means it should have as many rows as there are columns in A, and as many columns as there are rows in B.

But then, when you loop to fill it, this is your loop:

    for (rowCountProduct = 0; rowCountProduct < rowA; rowCountProduct++){
        for (columnCountProduct = 0; columnCountProduct < columnB; columnCountProduct++){
        ...
        }
    }

This means that you're trying to fill the rows in the product, which are supposed to be in the range 0rowCountProduct < columnA with values in the range 0rowCountProduct < rowA . Similarly, you run the columns to the range columnB instead of rowB as you defined it.

So you should either change the definition of your matrix, or change the way you fill your matrix up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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