Wednesday, September 27, 2023

Java Program to Multiply Two Matrices - Matrix Multiplication Example

How to write a Java program to multiply two matrices in Java is a very good programming exercise to get familiar with the two-dimensional array in Java. this example teaches about how to multiply arrays, how to access elements from a multi-dimensional array, how to pass them to a function etc. Since the matrix is a natural representation of a multi-dimensional array in Java, they are often used to illustrate real word matrix exercises e.g. the calculating sum of two matrices or calculating the difference of two matrices, etc. It's also one of the popular Matrix based coding problems you can find on coding interviews. 

By the way, before writing the program, let's recap how to multiply two matrices in mathematics first. If you remember, you can only multiply two matrices if, and only if, the number of columns in the first matrix equals the number of rows in the second matrix. That is known as the matrix multiplication criterion.

If both matrices don't satisfy that criterion then the product of two matrices is undefined. The Product matrix's dimensions will be equal to (rows of the first matrix) × (columns of the second matrix ). 

For example, if we multiply a 2×3 matrix with a 3×1 matrix, then the product matrix or result matrix will be a 2×1 matrix i.e. two rows and 1 column.

I used to remember this trick by writing dimensions of matrices adjacent to each other and canceling their matching dimension e.g. if you write 2x3 and 3x1, and then cancel 3 from each side you will get a matrix of dimension 2x1, which is basically the dimension of the product matrix.




How to multiply two matrices in Java? Example 

Here is a graphical representation of the matrix multiplication algorithm:

Java Program to Multiply Two Matrices - Matrix Multiplication Example

You can see that both matrices met the condition for multiplication i.e. columns of the first matrix are equal to rows of the second matrix. Then we multiply the first row of the first matrix to the first column of the second matrix and this gives us the first element of the first column of result matrix.

Similarly, when you multiply the second row of the first matrix to the first column of the second matrix you get the second element of the first column in the result matrix.


How to multiply two matrices in Java- Example

Here is our complete Java program to perform matrix multiplication. In this program, we first ask the user to enter two matrices. Since you cannot accept an array from the command line in Java (see here), we ask the user to first enter the number of rows and columns of the matrix and then ask him to populate the matrix.



Once we have both the matrices ready we first check whether they met the condition of matrix multiplication or not i.e. number of columns of the first matrix matches to the rows of the second matrix. 

As I said, we have used a two-dimensional array to represent a matrix in Java.

import java.util.Scanner;

/**
* Java program to calculate product of Two matrices in Java. In order to
* multiply two matrices, column of first matrix must be equal to rows
*  of the second
* matrix.
*
* @author Javin Paul
*/
public class MatrixMultiplication{

    public static void main(String args[]) {

        Scanner cmd = new Scanner(System.in);
        System.out.println("Enter the number of rows and columns of 
                             first matrix");

        int rowsOfFirstMatrix = cmd.nextInt();
        int columnsOfFirstMatrix = cmd.nextInt();
        int[][] aMatrix = new int[rowsOfFirstMatrix][columnsOfFirstMatrix];

        System.out.println("Enter the elements of first matrix");
        for (int i = 0; i < rowsOfFirstMatrix; i++) {
            for (int j = 0; j < columnsOfFirstMatrix; j++) {
                aMatrix[i][j] = cmd.nextInt();
            }
        }

        System.out.println("Enter the number of rows and columns of the
                                second matrix");
        int rowsOfSecondMatrix = cmd.nextInt();
        int columnsOfSecondMatrix = cmd.nextInt();

        // safety net - check order or each matrix, whether eligible for
        // multiplication or not
        while (columnsOfFirstMatrix != rowsOfSecondMatrix) {
            System.out.printf("Matrices with entered orders can't be
                         multiplied with each other, " 
                  + "columnsOfFirstMatrix [%d] != rowsOfSecondMatrix [%d] %n",
                    columnsOfFirstMatrix, rowsOfSecondMatrix);
            System.out.println("Enter the number of rows and columns of 
                                   second matrix");
            rowsOfSecondMatrix = cmd.nextInt();
            columnsOfSecondMatrix = cmd.nextInt();
        }

        int[][] bMatrix = new int[rowsOfSecondMatrix][columnsOfSecondMatrix];
        System.out.println("Enter numbers of second matrix");
        for (int i = 0; i < rowsOfSecondMatrix; i++) {
            for (int j = 0; j < columnsOfSecondMatrix; j++) {
                bMatrix[i][j] = cmd.nextInt();
            }
        }

        // calculating product of two matrices in Java
        int[][] product = product(aMatrix, bMatrix);
        System.out.println("Product of entered matrices:-");

        for (int i = 0; i < rowsOfFirstMatrix; i++) {
            for (int j = 0; j < columnsOfSecondMatrix; j++) {
                System.out.printf("%d ", product[i][j]);
            }
            System.out.printf("%n");
        }
        cmd.close();
    }

    /**
     * Method to calculate multiplication or product of two matrices.
     *
     * @param matrix1
     * @param matrix2
     * @return product of two matrix
     */
    public static int[][] product(int[][] matrix1, int[][] matrix2) {
        int columnsOfFirstMatrix = matrix1[0].length;
        int rowsOfSecondMatrix = matrix2.length;

        if (columnsOfFirstMatrix != rowsOfSecondMatrix) {
            throw new IllegalArgumentException(String.format("Can't multiply
                      matrices, columns of first matrix"
                    + " %d is not equal to rows of second matrix %d", 
                      columnsOfFirstMatrix, rowsOfSecondMatrix));
        }

        int rowsOfFirstMatrix = matrix1.length;
        int columnsofSecondMatrix = matrix2[0].length;
        int[][] product = new int[rowsOfFirstMatrix][columnsofSecondMatrix];

        for (int i = 0; i < rowsOfFirstMatrix; i++) {
            for (int j = 0; j < columnsofSecondMatrix; j++) {

                int sum = 0;
                for (int k = 0; k < rowsOfSecondMatrix; k++) {
                    sum = sum + matrix1[i][k] * matrix2[k][j];
                }

                product[i][j] = sum;
            }
        }

        return product;
    }

}


and here is the output of this program when you will run this on your favorite Java IDE e.g. Eclipse or IntelliJ IDEA or just from the command prompt:

Output:
Enter the number of rows and columns of the first matrix
2 3
Enter the elements of the first matrix
1 2 3
4 5 6
Enter the number of rows and columns of the second matrix
2 4
Matrices with entered orders can't be multiplied with each other, 
columnsOfFirstMatrix [3] != rowsOfSecondMatrix [2]
Enter the number of rows and columns of the second matrix
3 2
Enter numbers of the second matrix
7 8
9 10
11 12
The product of entered matrices:-
58 64
139 154

You can see that our first example was not perfect, the matrices we entered cannot be multiplied with each other because columns of the first matrix are not equal to rows of the second matrix.

That's all about how to do matrix multiplication in Java. This is a good programming exercise to learn and understand how to use two-dimensional arrays in Java, which is one of the key data structures, especially for the game development field. 

If you are just starting with programming and not familiar with key programming concepts then I also suggest you join these interactive programming courses which teach you the basics of programming in Java and other programming languages.


Other Java Programming exercises for beginners
  • How to transpose Matrix in Java? (program)
  • How to count vowels and consonants in given String in Java? (solution)
  • How to check if two rectangles intersect with each other in Java? (solution)
  • How to implement Linear Search in Java? (solution)
  • How to print Fibonacci series in Java (solution)
  • How to check if two given Strings are Anagram in Java? (solution)
  • How to calculate the square root of a given number in Java? (solution)
  • How to calculate the sum of all elements of an array in Java? (program)
  • How to implement binary search using recursion in Java? (solution)
  • How to check if given String is palindrome or not in Java? (solution)
  • How to remove duplicate characters from String in Java? (solution)
  • How to check if a year is a leap year in Java? (solution)
  • How to reverse words in a given String in Java? (solution)
  • How to check if given number is prime in Java (solution)
  • How to calculate Area of Triangle in Java? (program)
  • How to find if given Integer is Palindrome in Java? (solution)
  • How to find all permutations of a given String in Java? (solution)
  • How to check if a String contains duplicate characters in Java? (solution)
  • How to reverse an array in place in Java? (solution)
  • How to reverse a String in place in Java? (solution)
  • How to remove duplicate elements from the array in Java? (solution)
  • How to find the highest occurring word from a given file in Java? (solution)
  • How to calculate the average of all numbers of an array in Java? (program)
Thanks a lot for reading this article so far. If you find the solution of Matrix multiplication in Java and understand concept better then please share this Java tutorial with your friends and colleagues. If you have any questions feel free to ask in comments. 

P. S. - If you are new to Java and looking for free online courses and tutorials to learn Java then you can also checkout this list of 5 best Free Core Java Courses for Beginners. This list contains best free courses to learn Java online from Udemy and Coursera.

And lastly one question for you? Which one is your favorite matrix based coding problem? addition of matrix, multiplication, or transpose of matrix?

1 comment :

Anonymous said...

I was once asked to find the duplicate number inside a matrix of 3x3? Was bit easy to just go through all number and save them into a Set and find the duplicate but I got stuck when they say you cannot use Set but can use array. Anybody knows how to find duplicates by just using array?

Post a Comment