Java Program to Multiply Two Matrix Using Multi-dimensional Arrays
May 30, 2023
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:
Take the input for the size of the matrices A and B.
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).
Take the input for the values of the matrix A and matrix B.
Use nested for loops to iterate through each row and column of the matrices A and B respectively.
Multiply the corresponding elements of the row of A and column of B, and add the products to a running total.
Assign the running total to the corresponding element in the resulting matrix C.
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.
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;
}
// 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
Explanation : The main method creates two matrices, matrix1 and matrix2, and calls the multiply method to multiply them together. The add method checks that the no of columns of matrix 1 is equal to the number of rows of matrix 2, initializes a new matrix to hold the result, and then multiply the corresponding elements of the input matrices and stores the result in the corresponding element of the result matrix. Finally, the main method prints the result matrix.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment