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:
 
Matrix Multiplication in Java 
 
Multiplication is applicable, because the number of Columns (Matrix 1) == the number of Rows (Matrix 2).
 
Matrix Multiplication in Java 
 
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
 
Matrix Multiplication in Java 
 
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:
 Matrix Multiplication in Java
 
Code
  1. package matrixmultiplication;    
  2. import java.io.BufferedReader;    
  3. import java.io.IOException;    
  4. import java.io.InputStreamReader;    
  5.     
  6. public class MatrixMultiplication   
  7. {    
  8.     public static void main(String[] args) throws IOException     
  9.     {  
  10.         //BufferedReader to read text from character-InputStream and to buferring characters to provide efficient reading of characters    
  11.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    
  12.     
  13.         String temp;    
  14.         System.out.println("Insert rows in Matrix 1 >> ");    
  15.         //Reading the user input and copy into temp    
  16.         temp = br.readLine();    
  17.     
  18.         //Assigning the value of temp to integer variable M1Row (After parsing string as signed decimal integer)    
  19.         int M1Row = Integer.parseInt(temp);    
  20.     
  21.         System.out.println("Insert columns in Matrix 1 >> ");    
  22.         temp = br.readLine();    
  23.         int M1Col = Integer.parseInt(temp);    
  24.         System.out.println("Insert rows in Matrix 2 >> ");    
  25.         temp = br.readLine();    
  26.         int M2Row = Integer.parseInt(temp);    
  27.         System.out.println("Insert columns in Matrix 2 >> ");    
  28.         temp = br.readLine();    
  29.         int M2Col = Integer.parseInt(temp);    
  30.     
  31.         //Checking whether the multiplication is applicable    
  32.         if (M1Col == M2Row)     
  33.         {    
  34.             //Creating and initializing arrays to hold matrices    
  35.             int[][] Matrix1 = new int[M1Row][M1Col];    
  36.             int[][] Matrix2 = new int[M2Row][M2Col];    
  37.             int[][] Matrix3 = new int[M1Row][M2Col];    
  38.     
  39.             //Inserting values in Martix1    
  40.             System.out.println("Insert values in Matrix1:");    
  41.             for (int i = 0; i < M1Row; i++)   
  42.             {    
  43.                 for (int j = 0; j < M1Col; j++)   
  44.                 {    
  45.                     temp = br.readLine();    
  46.                     Matrix1[i][j] = Integer.parseInt(temp);    
  47.                 }    
  48.             }    
  49.             //Inserting values in Martix2    
  50.             System.out.println("Insert values in Matrix2:");    
  51.             for (int i = 0; i < M2Row; i++)   
  52.             {    
  53.                 for (int j = 0; j < M2Col; j++)   
  54.                 {    
  55.                     temp = br.readLine();    
  56.                     Matrix2[i][j] = Integer.parseInt(temp);    
  57.                 }    
  58.             }    
  59.     
  60.             //Multiplying two matrices    
  61.             System.out.println("Multiplying...");    
  62.             int temp1 = 0;    
  63.             for (int i = 0; i < M1Row; i++)   
  64.             {    
  65.                 for (int j = 0; j < M2Col; j++)   
  66.                 {    
  67.                     for (int k = 0; k < M2Row; k++)   
  68.                      {    
  69.                         temp1 = temp1 + Matrix1[i][k] * Matrix2[k][j];    
  70.                     }    
  71.                     //Inserting elements in result matrix    
  72.                     Matrix3[i][j] = temp1;    
  73.                     temp1 = 0;    
  74.                 }    
  75.             }    
  76.     
  77.             //Display result of Multiplication    
  78.             System.out.println("Multiplication Completed!");    
  79.             System.out.println("............................................");    
  80.             System.out.println("Result");    
  81.             for (int i = 0; i < M1Row; i++)   
  82.             {    
  83.                 for (int j = 0; j < M2Col; j++)   
  84.                 {    
  85.                     System.out.print(Matrix3[i][j] + "\t");    
  86.                 }    
  87.                 System.out.println();    
  88.             }    
  89.             System.out.println("............................................");    
  90.         }   
  91.         else   
  92.         {    
  93.             System.out.println("Multiplication could not be done!");    
  94.             System.out.println("Tip: Number of columns in a matrix1 should be equal to number of rows in matrix2 to perform multiplication.");    
  95.         }    
  96.     }    
  97. }    
Output in NetBeans IDE 7.0
 
Matrix Multiplication in Java 
 
Matrix Multiplication in Java 

Output in Command Prompt
 
Matrix Multiplication in Java 
 
Matrix Multiplication in Java 


Similar Articles