Matrix multiplication in Java
In this post, you will learn how to perform matrix multiplication using the Java programming language.
Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilized substantially more in everyday life than individuals would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop. It uses a matrix to process linear transformations to render images. In robotics and automation, matrices are the fundamental segments for the robot development. The contributions for controlling robots are acquired depending on the calculations from matrices.
The different operations on the matrix are also very important for us. Let us discuss how to perform matrix multiplication in Java.
Java matrix multiplication using for loop
A nested loop is a loop inside a loop. In the given Java program, we have used the nested for loops to iterate through each row and each column and multiply two matrices of 3 rows and 3 columns.
// Java matrix multiplication
public class MatrixMultiplication{
public static void main(String args[]){
//creating two matrices
int x[][]={{3,2,7},{1,6,2},{7,2,3}};
int y[][]={{3,2,8},{2,4,6},{2,3,6}};
//creating a new matrix to store the product of two matrices
int z[][]=new int[3][3];
// traverse each element of matrix and multiply them
// and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
z[i][j]=0;
for(int k=0;k<3;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
//printing matrix element
System.out.print(z[i][j]+" ");
}
System.out.println();
}
}
}
Output of the above code:
27 35 78
19 32 56
31 31 86
In the above example, we have taken two matrices that are to be multiplied and created a new empty matrix to store the product of the two matrices. Next, we used for loop to traverse each element of the two matrices, multiply them, and store the product in the new matrix at the corresponding index.
Java matrix multiplication using user inputs
In the given Java program, we ask the user to provide the number of rows and columns and their elements. Then perform matrix multiplication.
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
int row1 = 3, col1 = 3;
int row2 = 3, col2 = 3;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows in first matrix:");
row1 = sc.nextInt();
System.out.print("Enter number of columns in first matrix:");
col1 = sc.nextInt();
System.out.print("Enter number of rows in second matrix:");
row2 = sc.nextInt();
System.out.print("Enter number of columns in second matrix:");
col2 = sc.nextInt();
int a[][] = new int[row1][col1];
int b[][] = new int[row2][col2];
int c[][] = new int[row1][col2];
System.out.println("Enter values for matrix A : \n");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++)
a[i][j] = sc.nextInt();
}
System.out.println("Enter values for matrix B : \n");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++)
b[i][j] = sc.nextInt();
}
// Multiplying Two matrices
int[][] product = new int[row1][col2];
for(int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
// Displaying the result
System.out.println("Multiplication of two matrices: ");
for(int[] row : product) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
Output of the above code:
Enter number of rows in first matrix:2
Enter number of columns in first matrix:2
Enter number of rows in second matrix:2
Enter number of columns in second matrix:2
Enter values for matrix A :
3 5
3 7
Enter values for matrix B :
5 7
5 8
Multiplication of two matrices:
40 61
50 77
Java matrix multiplication using while loop
In the given example, we perform the matrix multiplication using a while loop in Java.
// Java matrix multiplication
// using while loop
public class MatrixMultiplication
{
public static void main(String args[])
{
// initializing variables
int i,j,k,sum;
int row1, row2, col1, col2;
row1 = row2 = col1 = col2 = 3;
//creating two matrices
int mat1[][]={{5,1,6},{2,6,3},{7,6,2}};
int mat2[][]={{2,3,7},{5,2,7},{4,2,8}};
//creating a new matrix to store the product of two matrices
int res[][]=new int[3][3];
i= 0 ;
while( i < row1 )
{
j= 0 ;
while( j < col2)
{
sum=0;
k= 0 ;
while(k < row2)
{
sum +=mat1[i][k]*mat2[k][j];
k++ ;
}
res[i][j]=sum;
j++;
}
i++;
}
i= 0 ;
while( i < row1 )
{
j=0 ;
while( j < col2 )
{
System.out.print(res[i][j]+" ");
j++;
}
System.out.println();
i++ ;
}
}
}
Output of the above code:
39 29 90
46 24 80
52 37 107
Java matrix multiplication using do while loop
In the given Java program, we have used the do while loop to perform the matrix multiplication.
import java.util.Scanner;
public class MatrixMultiplication
{
public static void main(String args[])
{
int r1, r2,c1,c2,i,j,k,sum;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of rows in matrix1:");
r1 = in.nextInt();
System.out.println("Enter number of columns in matrix1:");
c1 = in.nextInt();
System.out.println("Enter number of rows in matrix2:");
r2 = in.nextInt();
System.out.println("Enter number of columns in matrix2:");
c2 = in.nextInt();
if(c1==r2)
{
int mat1[][] = new int[r1][c1];
int mat2[][] = new int[r2][c2];
int res[][] = new int[r1][c2];
System.out.println("Enter values for matrix1:");
i= 0 ;
do
{
j= 0 ;
do
{
mat1[i][j] = in.nextInt();
j++ ;
} while( j < c1);
i++;
}while( i < r1 );
System.out.println("Enter values for matrix2:");
i= 0;
do
{
j= 0 ;
do
{
mat2[i][j] = in.nextInt();
j++ ;
}while( j < c2);
i++;
}while( i < r2 );
System.out.println("\n\nMatrix multiplication:");
i= 0 ;
do
{
j= 0 ;
do
{
sum=0;
k= 0 ;
do
{
sum +=mat1[i][k]*mat2[k][j] ;
k++ ;
}while(k < r2);
res[i][j]=sum;
j++;
}while( j < c2);
i++;
}while( i < r1 );
i= 0 ;
do
{
j=0 ;
do
{
System.out.print(res[i][j]+" ");
j++;
}while( j < c2 );
System.out.println();
i++ ;
}while( i < r1 );
}
else
System.out.print("Multipication does not exist.");
}
}
Output of the above code:
Enter number of rows in matrix1:2
Enter number of columns in matrix1:2
Enter number of rows in matrix2:2
Enter number of columns in matrix2:2
Enter values for matrix1:
8 6
8 9
Enter values for matrix2:
5 9
8 6
Matrix multiplication:
88 108
112 126
Related Articles
Java program to calculate the area of a circlePyramid pattern programs in Java
GCD of two numbers in Java
Sum of digits of a number in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number