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.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
-
- public class MatrixMultiplication
- {
- public static void main(String[] args) throws IOException
- {
-
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
- String temp;
- System.out.println("Insert rows in Matrix 1 >> ");
-
- temp = br.readLine();
-
-
- int M1Row = Integer.parseInt(temp);
-
- System.out.println("Insert columns in Matrix 1 >> ");
- temp = br.readLine();
- int M1Col = Integer.parseInt(temp);
- System.out.println("Insert rows in Matrix 2 >> ");
- temp = br.readLine();
- int M2Row = Integer.parseInt(temp);
- System.out.println("Insert columns in Matrix 2 >> ");
- temp = br.readLine();
- int M2Col = Integer.parseInt(temp);
-
-
- if (M1Col == M2Row)
- {
-
- int[][] Matrix1 = new int[M1Row][M1Col];
- int[][] Matrix2 = new int[M2Row][M2Col];
- int[][] Matrix3 = new int[M1Row][M2Col];
-
-
- System.out.println("Insert values in Matrix1:");
- for (int i = 0; i < M1Row; i++)
- {
- for (int j = 0; j < M1Col; j++)
- {
- temp = br.readLine();
- Matrix1[i][j] = Integer.parseInt(temp);
- }
- }
-
- System.out.println("Insert values in Matrix2:");
- for (int i = 0; i < M2Row; i++)
- {
- for (int j = 0; j < M2Col; j++)
- {
- temp = br.readLine();
- Matrix2[i][j] = Integer.parseInt(temp);
- }
- }
-
-
- 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];
- }
-
- Matrix3[i][j] = temp1;
- temp1 = 0;
- }
- }
-
-
- 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.");
- }
- }
- }
Output in NetBeans IDE 7.0
Output in Command Prompt