ARTICLE

# Matrix Multiplication in Java

Posted by | July 20, 2011
Tags:
This program is a good demonstration of 2 dimensional arrays in Java. Here, you'll learn how to implement matrix multiplication in Java.

Before you start writing code, you need to know how to multiply two matrices and what are the conditions to do so.
Consider these images:

Multiplication is applicable, because
the number of Columns (Matrix 1) == the number of Rows (Matrix 2).

Multiplication is not applicable, because
The number of Columns (Matrix 1) != the number of Rows (Matrix 2).

# Finding the order of Result Matrix (Rows x Columns)

Assume that the first matrix has 4 rows & 2 columns (4 x 2) and the second matrix has 2 rows and 3 columns (2 x 3). Now, when these two matrices are multiplied, the result matrix of order (4 x 3) is produced.

# How to multiply two matrices

The image shown above shows the multiplication process of two matrices.

# What does this program do?

This program is a demonstration of Matrix Multiplication in Java. The order of both matrices and elements in each matrix are inserted by the user. The order of matrix determines the possible number of elements in the matrix. For example, A matrix of 4 rows and 4 columns contains 16 elements and these elements are reside in the array as shown in the following image:

# Code

package matrixmultiplication;
import java.io.IOException;

public class MatrixMultiplication {
public static void main(String[] args) throws IOException {

String temp;
System.out.println("Insert rows in Matrix 1 >> ");

//Reading the user input and copy into temp

//Assigning the value of temp to integer variable M1Row (After parsing string as signed decimal integer)
int M1Row = Integer.parseInt(temp);

System.out.println("Insert columns in Matrix 1 >> ");
int M1Col = Integer.parseInt(temp);
System.out.println("Insert rows in Matrix 2 >> ");
int M2Row = Integer.parseInt(temp);
System.out.println("Insert columns in Matrix 2 >> ");
int M2Col = Integer.parseInt(temp);

//Checking whether the multiplication is applicable
if (M1Col == M2Row) {

//Creating and initializing arrays to hold matrices
int[][] Matrix1 = new int[M1Row][M1Col];
int[][] Matrix2 = new int[M2Row][M2Col];
int[][] Matrix3 = new int[M1Row][M2Col];

//Inserting values in Martix1
System.out.println("Insert values in Matrix1:");
for (int i = 0; i < M1Row; i++) {
for (int j = 0; j < M1Col; j++) {
Matrix1[i][j] = Integer.parseInt(temp);
}
}

//Inserting values in Martix2
System.out.println("Insert values in Matrix2:");
for (int i = 0; i < M2Row; i++) {
for (int j = 0; j < M2Col; j++) {
Matrix2[i][j] = Integer.parseInt(temp);
}
}

//Multiplying two matrices
System.out.println("Multiplying...");
int temp1 = 0;
for (int i = 0; i < M1Row; i++) {
for (int j = 0; j < M2Col; j++) {
for (int k = 0; k < M2Row; k++) {
temp1 = temp1 + Matrix1[i][k] * Matrix2[k][j];
}
//Inserting elements in result matrix
Matrix3[i][j] = temp1;
temp1 = 0;
}
}

//Display result of Multiplication
System.out.println("Multiplication Completed!");
System.out.println("............................................");
System.out.println("Result");
for (int i = 0; i < M1Row; i++) {
for (int j = 0; j < M2Col; j++) {
System.out.print(Matrix3[i][j] + "\t");
}
System.out.println();
}
System.out.println("............................................");
} else {
System.out.println("Multiplication could not be done!");
System.out.println("Tip: Number of columns in a matrix1 should be equal to number of rows in matrix2 to perform multiplication.");
}
}
}

1.

2.

1.

2.

COMMENT USING