Matrix Multiplication Program
Consider the program of matrix multiplication in which the two matrices are multiplied with the use of three nested loops. We can multiply two matrices in java using binary * operator and executing another loop. A matrix is also known as array of arrays. We can add, subtract and multiply matrices.
In case of matrix multiplication, one row element of first matrix is multiplied by all columns of second matrix. Let the first matrix be of the order 3X3 and second matrix be of the order of 3X2. Since both the no. of columns in first matrix and no. of rows in second matrix are equal i.e. 3, the two matrices can be multiplied with each other and the resultant matrix will be of the order 3X2 (rows in first matrix and columns in second matrix).
import java.util.Scanner;
class MatrixMulDemo
{
public static void main(String[] args)
{
//make a scanner for rows and columns
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of rows for first matrix");
//store rows in a variable
int rowsFirst = scan.nextInt();
System.out.println("Enter the number of columns for first matrix / rows of
second matrix (they must be same for matrix multiplication");
//store columns in a variable
int colFirstRowSec = scan.nextInt();
System.out.println("Enter the columns for second matrix");
//store columns of second matrix in a variable
int colSec = scan.nextInt();
//make two dimensional arrays for storing the matrices elements
int[][] first = new int[rowsFirst][colFirstRowSec];
int[][] second = new int[colFirstRowSec][colSec];
System.out.println("Enter the values of first matrix");
for(int i=0; i<first.length; i++)
{
for(int j=0; j<first[0].length; j++)
{
first[i][j] = scan.nextInt();
}
}
System.out.println("Enter the values of second matrix");
for(int i=0; i<second.length; i++)
{
for(int j=0; j<second[0].length; j++)
{
second[i][j] = scan.nextInt();
}
}
//define the third resultant matrix
int result[][] = new int[rowsFirst][colSec];
//multiply the two matrices using three nested loops
for(int i=0; i<rowsFirst; i++)
{
for(int j=0; j<colSec; j++)
{
for(int k=0; k<colFirstRowSec; k++)
{
result[i][j] = result[i][j] + first[i][k] * second
[k][j];
}
}
}
//print all the elements of result matrix
System.out.println("Product of two matrices ");
for(int i=0; i<result.length; i++)
{
for(int j=0; j<result[0].length; j++)
{
System.out.print(result[i][j] + " ");
}
//for line gap
System.out.println();
}
}
}
Alternative Solution
Let's see a simple example to multiply two matrices of 3 rows and 3 columns.
public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}}
Output:-
6 6 6
12 12 12
18 18 18
Additional Coding Logic
public class MultiplyMatrices {
public static void main(String[] args) {
int r1 = 2, c1 = 3;
int r2 = 3, c2 = 2;
int[][] firstMatrix = { {3, -2, 5}, {3, 0, 4} };
int[][] secondMatrix = { {2, 3}, {-9, 0}, {0, 4} };
// Mutliplying Two matrices
int[][] product = new int[r1][c2];
for(int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
product[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
// Displaying the result
System.out.println("Multiplication of two matrices is: ");
for(int[] row : product) {
for (int column : row) {
System.out.print(column + " ");
}
System.out.println();
}
}
}
Multiplication of two matrices is: 24 29 6 25
In general,
M(ixj) * M(jxk) = M(ixk)
To multiply the two matrices we need to multiply the rows of first matrix to the columns of second matrix as shown below in the diagram.