When you multiply two matrices with each other, you actually do a “**dot product**” of rows and columns.

**As example** if you are multipying a 3X3 matrix with a 3X2 matrix -

**Matrix multiplication**

The result you get can be explained as follows -

s11 = r11Xp11 + r12Xp21 + r13Xp31 s12 = r11Xp12 + r12Xp22 + r13Xp32 s21 = r21Xp11 + r22Xp21 + r23Xp31 s22 = r21Xp12 + r22Xp22 + r23Xp32 s31 = r31Xp11 + r32Xp21 + r33Xp31 s32 = r31Xp12 + r32Xp22 + r33Xp32

When you are writing a Java program to **multiply two matrices** -

You need an outer loop that will run as many times as there are rows in the first matrix.

Then you’ll have a second loop that will run as many times as the number of columns in the second matrix.

Then you’ll have a third loop that will run as many times as there are columns in the first matrix.

Also remember these points when multiplying one matrix with another -

- The number of columns of the first matrix is equal to the number of rows of the second matrix.
- The resultant matrix will have the same number of rows as in the first matrix and same number of columns as in the second matrix.

__Matrix multiplication Java program__

import java.util.Scanner; public class MatixMultiplication { public static void main(String[] args) { int rowM1, colM1; int rowM2, colM2; Scanner in = new Scanner(System.in); System.out.print("Enter Number of Rows and Columns of First Matrix : "); rowM1 = in.nextInt(); colM1 = in.nextInt(); System.out.print("Enter elements of First Matrix : "); int M1[][] = new int[rowM1][colM1]; for(int i = 0; i < rowM1; i++){ for(int j = 0; j < colM1; j++){ M1[i][j] = in.nextInt(); } } System.out.println("First Matrix : " ); for(int i = 0; i < rowM1; i++){ for(int j = 0; j < colM1; j++){ System.out.print(" " +M1[i][j]+"\t"); } System.out.println(); } System.out.print("Enter Number of Rows and Columns of Second Matrix : "); rowM2 = in.nextInt(); colM2 = in.nextInt(); if(colM1 != rowM2){ throw new IllegalArgumentException("The number of columns of the first matrix should equal the number of rows of the second matrix."); } System.out.print("Enter elements of Second Matrix : "); int M2[][] = new int[rowM2][colM2]; for(int i = 0; i < rowM2; i++){ for(int j = 0; j < colM2; j++){ M2[i][j] = in.nextInt(); } } System.out.println("Second Matrix : " ); for(int i = 0; i < rowM2; i++){ for(int j = 0; j < colM2; j++){ System.out.print(" " +M2[i][j] + "\t"); } System.out.println(); } //same number of rows as in the first matrix and //same number of columns as in the second matrix int resMatrix[][] = new int[rowM1][colM2]; int sum = 0; int row = 0; for(int i = 0; i < rowM1; i++){ for(int j = 0; j < colM2; j++){ sum = 0; for(int k = 0; k < colM1; k++){ sum = sum + M1[i][k] * M2[k][j]; } resMatrix[i][j] = sum; } } System.out.println("Result Matrix : " ); for(int i = 0; i < resMatrix.length; i++){ for(int j = 0; j < colM2; j++){ System.out.print(" " +resMatrix[i][j]+"\t"); } System.out.println(); } } }

__Output__

Enter Number of Rows and Columns of First Matrix : 2 3 Enter elements of First Matrix : 1 2 3 4 5 6 First Matrix : 1 2 3 4 5 6 Enter Number of Rows and Columns of Second Matrix : 3 2 Enter elements of Second Matrix : 7 8 9 10 11 12 Second Matrix : 7 8 9 10 11 12 Result Matrix : 58 64 139 154

That's all for this topic **Matrix Multiplication Java Program**. If you have any doubt or any
suggestions to make please drop a comment. Thanks!

__Related Topics__

**You may also like - **

## No comments:

## Post a Comment