Matrix Multiplication Java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn matrix multiplication java.

Remember matrix multiplication we used to do in school…(maybe 5th or 6th grade).

Also Read – Char Array to String Java Program

Basically to perform a matrix multiplication in java column of the first matrix should be equal to row of the second matrix. Let’s get into code implementation of java matrix,

Example: Matrix Multiplication Java

Matrix Multiplication Java

import java.util.Scanner;

public class MatrixMultiplicationInJava
{
   public static void main(String[] args)
   {

      int x;

      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter base of squared matrix: ");
      x = sc.nextInt();

      int l[][] = new int[x][x];
      int m[][] = new int[x][x];
      int n[][] = new int[x][x];

      System.out.println("Please enter elements of first matrix(rowwise)");
      for(int a = 0; a < x; a++)
      {
         for(int b = 0; b < x; b++)
         {
            l[a][b] = sc.nextInt();
         }
      }

      System.out.println("Please enter elements of second matrix(rowwise)");
      for(int a = 0; a < x; a++)
      {
         for(int b = 0; b < x; b++)
         {
            m[a][b] = sc.nextInt();
         }
      }

      System.out.println("Matrix Multiplication in Java...");
      for(int a = 0; a < x; a++)
      {
         for(int b = 0; b < x; b++)
         {
            for(int c = 0; c < x; c++)
            {
               n[a][b] = n[a][b] + l[a][c] * m[c][b];
            }
         }
      }

      System.out.println("Matrix Multiplication Java : ");
      for(int a = 0; a < x; a++)
      {
         for(int b = 0; b < x; b++)
         {
            System.out.println(n[a][b] + " ");
         }
         System.out.println();
      }
      sc.close();
   }
}

Output:

Please enter base of squared matrix :  3
Please enter elements of first matrix(rowwise)
8 2 9
2 8 1
2 8 7
Please enter elements of second matrix(rowwise)
2 5 7
6 7 2
3 5 7
Matrix Multiplication in Java…
Matrix Multiplication Java :
55 99 123
55 71 37
73 101 79


Matrix multiplication in java – another way

Now let’s learn java matrix multiplication in java using recursion. Here’s the example for java recursive code for matrix multiplication,

public class MatrixMultiplicationJava 
{
   // static variables
   public static int MAX = 100; 
   public static int a = 0, b = 0, c = 0;

   // multiply matrices
   static void multiplyMatrices(int rowOne, int colOne, int X[][], int rowTwo, int colTwo, int Y[][], int Z[][])
   {
      // if rows are traversed
      if(a >= rowOne)
      return;

      // if a is lesser than rowOne
      if(b < colTwo)
      {
         if(c < colOne)
         {
            Z[a][b] += X[a][c] * Y[c][b];
            c++;

            multiplyMatrices(rowOne, colOne, X, rowTwo, colTwo, Y, Z);
         }

         c = 0;
         b++;
         multiplyMatrices(rowOne, colOne, X, rowTwo, colTwo, Y, Z);
      }

      b = 0;
      a++;
      multiplyMatrices(rowOne, colOne, X, rowTwo, colTwo, Y, Z);
   }

   // function for multiplying matrices java
   static void multiplyMatrix(int rowOne, int colOne, int X[][], int rowTwo, int colTwo, int Y[][])
   {
      if(rowTwo != colOne)
      {
         System.out.println("Opps!!! not Possible!!!\n");
         return;
      }

      int[][] Z = new int[MAX][MAX];

      multiplyMatrices(rowOne, colOne, X, rowTwo, colTwo, Y, Z);

      // multiply two matrices in java
      for(int m = 0; m < rowOne; m++)
      {
         for(int n = 0; n < colTwo; n++)
            System.out.print(Z[m][n] + " ");

         System.out.println();
      }
   }

   public static void main(String[] args) 
   {
      int rowOne = 3, colOne = 3, rowTwo = 3, colTwo = 3;
      int A[][] = { {1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}};

      int B[][] = { {9, 8, 7},
                    {6, 5, 4},
                    {3, 2, 1} };

      multiplyMatrix(rowOne, colOne, A, rowTwo, colTwo, B);
   }
}

Output:

30 24 18
84 69 54
138 114 90


conclusion

That’s it guys. Here in this post you learnt matrix multiplication program in java. I hope you guys have understood matrix in java and matrix multiplication in java.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like