Java Program to Multiply Two Matrix Using Multi-dimensional Arrays

Java Program to Multiply Two Matrix Using Multi-dimensional Arrays

What is Matrix ?

In this Article, We are going to discuss the Program to Multiply Two Matrix Using Multi-dimensional Arrays in java.

In mathematics, a matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. Matrices are often used to represent systems of linear equations, and they can be used to represent transformations in geometry and physics.

Operations on matrix :

Matrices can be added, subtracted, and multiplied by scalars (numbers). They can also be multiplied by other matrices, using a process called matrix multiplication. Matrix multiplication is different from the multiplication of two numbers, and it has its own set of rules.

Algorithm To Multiply two matrices in Java:

  1. Take the input for the size of the matrices A and B.
  2. Create multi-dimensional arrays A and B of the input size, and an empty multi-dimensional array C with size (row of A x column of B).
  3. Take the input for the values of the matrix A and matrix B.
  4. Use nested for loops to iterate through each row and column of the matrices A and B respectively.
  5. Multiply the corresponding elements of the row of A and column of B, and add the products to a running total.
  6. Assign the running total to the corresponding element in the resulting matrix C.
  7. After all elements of C are computed, print the resulting matrix.

Condition For Matrix Multiplication:

  • The number of columns of first matrix must be equal to the number of rows of second matrix for matrix multiplication and the resultant matrix should be initialized with the number of rows and columns as the number of rows of first matrix and the number of columns of second matrix respectively.

Example:

int[][] matrix1 = { {3, -2, 5}, {3, 0, 4} };
int[][] matrix2 = { {2, 3}, {-9, 0}, {0, 4} };

// matrix1[0].length == matrix.length

Pseudo code to demonstrates how to Multiply two matrices in Java:

public int[][] multiply(int[][] matrix1, int[][] matrix2){
    
    // Check if we can perform multiplication or not
    if (matrix1[0].length != matrix2.length){
        throw new IllegalArgumentException("No of Columns of Matrix 1 should equal to the not of Rows of Matrix 2");
    }

    // Initialize a new matrix to hold the result
    int[][] result = new int[matrix1.length][matrix1[0].length];

    // Iterate over the elements of the matrices and multiply them
    for(int i = 0; i < matrix1.length; i++) {
        for (int j = 0; j < matrix2[0].length; j++) {
            for (int k = 0; k < matrix1[0].length; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    // Return the result
    return result;
}

Example :

Run
// importing all the required packages
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // Initializing two Matrices
        int[][] matrix1 = { {3, -2, 5}, {3, 0, 4} };
        int[][] matrix2 = { {2, 3}, {-9, 0}, {0, 4} };
        
        if (matrix1[0].length != matrix2.length){
            throw new IllegalArgumentException("No of Columns of Matrix 1 should equal to the not of Rows of Matrix 2");
        }

        // Initialize a new matrix to hold the result
        int[][] result = new int[matrix1.length][matrix2[0].length];

        // Iterate over the elements of the matrices and multiply them
        for(int i = 0; i < matrix1.length; i++) {
            for (int j = 0; j < matrix2[0].length; j++) {
                for (int k = 0; k < matrix1[0].length; k++) {
                    result[i][j] += matrix1[i][k] * matrix2[k][j];
                }
            }
        }
        
        // Print the result
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Output:

24 29 
6 25

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription