Matrix in Java

The matrix in Java | Matrices are the two-dimensional arrays. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called an m × n matrix. Individual entries in the matrix are called elements and can be represented by aij which suggests that the element a is present in the ith row and jth column. 

Create Matrix in Java

Before using the matrix we must declare the variable. The full syntax to declare the matrix is:- <Accessibility-modifier><Execution-level-Modifiers> <datatype>[][] <array variable name>;

In this syntax the accessibility-modifier and execution-level-modifiers are optional, but the remaining are mandatory. Example of declaring matrix:-

int[][] mtrx1;
float[][] mtrx2;
public static int[][] mtrx3;
public static float[][] mtrx4;
matrix

In this matrix Row-1 contains {a00, a01, a02}, and the column-1 contains {a00, a10, a20}

Initialize Matrix in Java

We can initialize a matrix in 3 ways,

  • Matrix initialization with explicit values.
  • Matrix initialization with default values (i.e. without explicit value).
  • Using Anonymous Array.

1) Matrix initialization with explicit values.

Full syntax:- <Accessibility modifier><Execution-level Modifiers> <datatype>[][] <matrix-variable-name> = {<list-of-array-elements>};

For example:-

int[][] mtrx1 = {{5,6},{7,8}};
public static int[][] mtrx2 = {{5,6},{7,8}};

In this way of matrix initialization, the matrix is declared and initialized in the same line, because array constants can only be used in initializers. The accessibility modifier and execution level modifiers are optional. We can use this approach only if the values are known at the time of matrix declaration.

Note:- If a matrix is having row and column equal then it is called square matrix, else the matrix is called non-square matrix. For example:- 2×2, 3×3, 4×4 are called square matrices, but 2×3, 3×2, 2×4, 4×2, 4×3 are called non-square matrices.

2) Matrix initialization with default values or without explicit values.

In this way, we can declare the matrix and initialize it later. Syntax:- <matrix-variable-name> = new <datatype>[<row-size>][<column-size>];

Example:-

// declare a matrix
int[][] mtrx = null;
// initialize matrix with default values
mtrx = new int[3][2];

In this example, first, we had declared a matrix then we had initialized it with the default value. It is a 3×2 non-square matrix. Since it is a matrix of int data type and the default value for int is 0, therefore this matrix is initialized with 0. Later we can update the values by accessing the array elements.

Instead of two lines, we can declare and initialize the matrix within a single line. Example:- 

int[][] mtrx = new int[3][2];

From this example we can say,

  • The row size of the matrix is 3
  • Column size of the matrix is 2

3) Initialize matrix using Anonymous Array. Example:-

// declare a matrix
int[][] mtrx = null;
// create matrix with explicit values
// (anonymous array)
mtrx = new int[][] {{5,6},{7,8}};

Advantage of this approach:- In the first way of matrix array initialization i.e. while initializing the matrix array with explicit values, we must declare and initialize the matrix in a single line else we will get an error. But using this approach we can declare and initialize with explicit value at different lines.

Access Modify Matrix

Individual entries in the matrix are called elements and can be represented by aij which suggests that the element a is present in the ith row and jth column. 

The matrix can be accessed using <matrix-variable><row-index><column-index>. Now, let us see it through an example,

Declare 3×2 matrix and initialize it with default value,

int[][] mtrx = new int[3][2];

Accessing and updating above array,

mtrx[0][0] = 50;
mtrx[0][1] = 60;
mtrx[1][0] = 70;
mtrx[1][1] = 80;
mtrx[2][0] = 90;
mtrx[2][1] = 10;

It is similar to,

int[][] mtrx = {{50,60},{70,80},{90,10}};

Length or Size of Matrix in Java

To access the length or size of row use <matrix-variable>.length, example:- mtrx.length. Similarly to access the column size use <matrix-variable>[row-index].length. Now let us see it through a Java program,

Java Program to Find the Length or Size of a Matrix

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

    // declare a matrix
    // and initialize with explicit values
    int[][] mtrx = 
      { { 50, 60, 70 }, { 80, 90, 10 }, { 20, 30, 40 } };

    System.out.println("Row size = " + mtrx.length);
    System.out.println("Column-1 size = " + mtrx[0].length);
    System.out.println("Column-2 size = " + mtrx[1].length);
    System.out.println("Column-3 size = " + mtrx[2].length);
  }
}

Output:-

Row size = 3
Column-1 size = 3
Column-2 size = 3
Column-3 size = 3

Print Matrix in Java

To display a matrix we can use loops or pre-defined methods. Since it is a two-dimensional array (i.e. 2 levels) therefore it required two loops to access the elements. The loop can be for-loop, for-each loop, while loop, or do-while loop. While using loops we will use the length property of the Java 2D array.

Print Matrix in Java using for loop

public class Matrix {
  public static void main(String[] args) {
    
    int[][] mtrx = 
      { { 50, 60, 70 }, { 80, 90, 10 }, { 20, 30, 40 } };

    // display matrix using for loop
      for (int i = 0; i < mtrx.length; i++) {
        for (int j = 0; j < mtrx[i].length; j++) {
          System.out.print(mtrx[i][j] + " ");
        }
        System.out.println(); // new line
      }
  }
}

Output:-

50 60 70
80 90 10
20 30 40

Java Program to Print Matrix in Java using for-each loop

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

    int[][] mtrx = 
      { { 50, 60, 70 }, { 80, 90, 10 }, { 20, 30, 40 } };

    // display matrix using for-each loop
    for (int[] i : mtrx) {
      for (int j : i) {
        System.out.print(j + " ");
      }
      System.out.println(); // new line
    }
  }
}

Output:-

50 60 70
80 90 10
20 30 40

Java Program to Print Matrix in Java using Arrays.deepToString() method

In addition to these ways, we have the Arrays class defined in the java.util package. The java.util.Arrays class contains many methods related to array operations like sort an array using sort(), copy an array copyOf() or copyOfRange(), search an element in array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java. It contains a toString() method to convert arrays to String. In this class deepToString() method is given to display the multidimensional array.

import java.util.Arrays;
public class Matrix {
  public static void main(String[] args) {

    int[][] mtrx = 
      { { 50, 60, 70 }, { 80, 90, 10 }, { 20, 30, 40 } };

    // display matrix using Arrays.deepToString()
    System.out.println(Arrays.deepToString(mtrx));
  }
}

Output:-

[[50, 60, 70], [80, 90, 10], [20, 30, 40]]

Matrix Example in Java

Let us develop a Java program to read input for a matrix from the end-user and display it to the screen. To read you can use command-line argument, Scanner class, BuffereReader class, or others. And to display you can use a loop or pre-defined method Arrays.deepToString(). In this program, we will use a Scanner class and for-each loop.

import java.util.Scanner;

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

    // declare variables
    int[][] mtrx = null; 
    int row = 0;
    int column = 0;
    Scanner scan = null;

    // create Scanner class object to read input
    scan = new Scanner(System.in);

    // read row and column size
    System.out.print("Enter row and column size: ");
    row = scan.nextInt();
    column = scan.nextInt();

    // initialize matrix
    mtrx = new int[row][column];

    // read input for matrix
    System.out.println("Enter matrix elements,");
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < column; j++) {
        System.out.print("Element["+i+"]["+j+"]: ");
        mtrx[i][j] = scan.nextInt();
      }
    }

    // display matrix using for-each loop
    System.out.println("\nEntered matrix is,");
    for (int[] i : mtrx) {
      for (int j : i) {
        System.out.print(j + " ");
      }
      System.out.println(); // new line
    }

    // close Scanner
    scan.close();
  }
}

Output:-

Enter row and column size: 3 2
Enter matrix elements,
Element[0][0]: 5
Element[0][1]: 10
Element[1][0]: 15
Element[1][1]: 20
Element[2][0]: 25
Element[2][1]: 30

Entered matrix is,
5 10
15 20
25 30

Pass and Return Matrix in Java

Let us demonstrate a Java program to see how to pass a matrix to a method and how to return a matrix from a method?

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

    // declare a matrix variable
    int[][] mtrx = null;

    // create matrix with 3x3 size
    mtrx = new int[2][2];

    // take input for matrix
    mtrx = readMatrix();

    // display matrix
    displayMatrix(mtrx);
  }

  // method to return matrix
  public static int[][] readMatrix() {
    // read input from end-user or
    // initialize explicitly
    int[][] temp = { { 9, 8 }, { 2, 3 } };

    // return matrix
    return temp;

    // Or,
    // directly return anonymous matrix
    // return new int[][] {{5,6},{7,8}};
  }

  // method to take matrix and display it
  public static void displayMatrix(int[][] mtrx) {
    System.out.println("Matrix,");
    for (int[] i : mtrx) {
      for (int j : i) {
        System.out.print(j + " ");
      }
      System.out.println(); // new line
    }
  }
}

Output:-

Matrix,
9 8
2 3

Matrix Programs in Java,

  1. Program to Print 3×3 Matrix
  2. Sum of matrix elements
  3. Sum of Diagonal Elements of Matrix
  4. Row sum and column sum of a matrix
  5. Matrix Addition Program
  6. Subtraction of two matrices
  7. Transpose of a Matrix
  8. Matrix Multiplication Program
  9. Menu-driven program for Matrix operations

The above programs are discussed in detail in their own post. Here we will develop only the required method.


Sum of Matrix Elements in Java

Program description:- Write a Java program to find the sum of matrix elements and display the result. 

To solve this problem we will take a matrix, a sum variable to store the result value, iterate through the matrix using a loop and add an element of the matrix to the sum variable.

Java Program to Find Sum of Matrix Elements

public class Matrix {
  // main method
  public static void main(String[] args) {

    // declare and initialize a matrix
    int a[][] = { { 1, 3 }, { 7, 5 } };

    // find sum of matrix elements
    int sum = matrixSum(a);
    
    // display result
    System.out.println("The sum of matrix elements = " + sum);
  }

  // method to find sum of matrix elements
  public static int matrixSum(int[][] a) {
    int sum = 0;
    for (int[] row : a) {
      for (int element : row) {
        sum += element;
      }
    }
    return sum;
  }
}

Output:-

The sum of matrix elements = 16

Sum of Diagonal Elements of a Matrix in Java

Write a Java program to find the sum of diagonal elements of a matrix. The elements located at aij where i=j is called diagonal elements. Example a11, a22, a33 are diagonal elements of the matrix.

Example with an matrix,
1 2 3
4 5 6
7 8 9

Here the diagonal elements are:- 1, 5, and 9
Therefore the sum of diagonal elements = 1+5+9 = 15

This operation can’t be performed by using a for-each loop because we need an index. Therefore we will use a for loop.

Java program to find the sum of diagonal elements of a matrix 

public class Matrix {

  // main method
  public static void main(String[] args) {

    // declare and initialize matrix
    int a[][] =  { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    // find sum of diagonal elements
    int sum = diagonalSum(a);
    
    // display result
    System.out.println("Sum of diagonal elements = " + sum);
  }

  // method to find sum of diagonal elements of matrix
  public static int diagonalSum(int[][] a) {
    int sum = 0;
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a[i].length; j++) {
        if(i == j) sum += a[i][j];
      }
    }
    return sum;
  }
}

Output:-

Sum of diagonal elements = 15

Addition of Two Matrix in Java

The sum of two matrices of the same size is obtained by adding elements in the corresponding positions. See:- matrix addition in Java by taking input from the end-user.

Let A =[aij] and B =[bij] be m × n matrices. The sum of A and B, denoted by A + B, is the m × n matrix that has aij + bij as its (i, j)th element. In other words, A + B =[aij + bij].

Condition for matrix addition:- Both matrices should have the same row and column size.

Matrices of different sizes cannot be added, because the sum of two matrices is defined only when both matrices have the same number of rows and the same number of columns.

import java.util.Arrays;

public class Matrix {

  // main method
  public static void main(String[] args) {

    // declare and initialize two matrices
    int a[][] = { { 1, 3 }, { 7, 5 } };
    int b[][] = { { 6, 8 }, { 4, 2 } };

    // find row and column size
    // (Assuming both matrix have same size)
    int row = a.length;
    int column = a[0].length;

    // declare new matrix to store result
    int c[][] = new int[row][column];

    // sum of matrix
    c = addMatrix(a, b);

    // display all matrices
    System.out.println("A = " + Arrays.deepToString(a));
    System.out.println("B = " + Arrays.deepToString(b));
    System.out.println("C = " + Arrays.deepToString(c));
  }
  
  // method to calculate the addition of two matrix
  public static int[][] addMatrix(int[][] a, int[][] b) {

    // calculate row and column size
    int row = a.length;
    int column = a[0].length;
    
    // declare a matrix to store resultant
    int sum[][] = new int[row][column];
    
    // calculate sum of two matrices
    // outer loop for row
    for(int i=0; i<row; i++) {
      // inner loop for column
      for(int j=0; j<column; j++) {
        // formula
        sum[i][j] = a[i][j] + b[i][j];
      }
    }
    
    // return resultant matrix
    return sum;
  }
}

Output:-

A = [[1, 3], [7, 5]]
B = [[6, 8], [4, 2]]
C = [[7, 11], [11, 7]]

Subtraction of Two Matrix in Java

The matrix subtraction is very similar to the matrix addition program. Just subtract the matrix elements in place of addition. See Java program for matrix subtraction by taking input from the end-user. The method for matrix subtraction can be written as,

// method to calculate the subtraction of two matrix
public static int[][] subMatrix(int[][] a, int[][] b) {
  // calculate row and column size
  int row = a.length;
  int column = a[0].length;

  // declare a matrix to store resultant
  int sub[][] = new int[row][column];

  // calculate subtraction of two matrices
  // outer loop for row
  for (int i = 0; i < row; i++) {
    // inner loop for column
    for (int j = 0; j < column; j++) {
      // formula
      sub[i][j] = a[i][j] - b[i][j];
    }
  }

  // return resultant matrix
  return sub;
}

The passed matrices are,

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

Output:-

A = [[1, 3], [7, 5]]
B = [[6, 8], [4, 2]]
C = [[-5, -5], [3, 3]]

Matrix Multiplication in Java

Let A be an m×k matrix and B be a k ×n matrix. The product of A and B, denoted by AB, is the m × n matrix with its (i, j )th entry equal to the sum of the products of the corresponding elements from the ith row of A and the jth column of B. In other words, if AB =[cij], then cij = ai1b1j + ai2b2j +···+aikbkj.

Condition for the Matrix multiplication:- The product of two matrices is not defined when the number of columns in the first matrix and the number of rows in the second matrix are not the same. The matrix multiplication is Java with user-input is discussed in details here

Java method to find matrix multiplication of two square matrix

// method to calculate product of two matrix
public static int[][] multiplyMatrix(int[][] a, int[][] b) {

   // find size of matrix
   // (Assuming both matrix is square matrix
   // of same size)
   int size = a.length;

   // declare new matrix to store result
   int product[][] = new int[size][size];

   // find product of both matrices
   // outer loop 
   for (int i = 0; i < size; i++) {

     // inner-1 loop 
     for (int j = 0; j < size; j++) {

       // assign 0 to the current element
       product[i][j] = 0;

       // inner-2 loop 
       for (int k = 0; k < size; k++) {
         product[i][j] += a[i][k] * b[k][j];
       }
     }
   }

   return product;
}

The passed matrices are,

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

Output:-

C (Product) = [[18, 14], [62, 66]]

Transpose of a Matrix in Java

Let A =[aij] be an m × n matrix. The transpose of A, denoted by At, is the n × m matrix obtained by interchanging the rows and columns of A. In other words, if At =[bij], then bij = aji for i = 1,2,…,n and j = 1,2,…,m. The transpose of a matrix in Java is discussed in detailed here.

For 3×2 Matrix,

Original Matrix
a11 a12
a21 a22
a31 a32

Transpose Matrix
a11 a21 a31
a12 a22 a32

Java Method to find transpose of a Matrix

// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {

   // calculate row and column size
   int row = a.length;
   int column = a[0].length;

   // declare a matrix to store resultant
   int temp[][] = new int[row][column];

   // calculate transpose of matrix
   // outer loop for row
   for (int i = 0; i < row; i++) {
     // inner loop for column
     for (int j = 0; j < column; j++) {
       // formula
       temp[i][j] = a[j][i];
     }
   }

   // return resultant matrix
   return temp;
}

The passed matrix is,

int a[][] = { { 1, 2 }, { 8, 9 } };

Output:-

Transpose = [[1, 8], [2, 9]]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply