Matrix Programs in Java

Matrix Programs in Java | In Java, the matrices are the two-dimensional arrays. It 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. 

Before solving matrix programs in Java, you should have knowledge of how to declare and initialize a matrix in Java, how to take input for a matrix from the end-user, and what are the different ways to display it. How to find the length or size of a matrix in Java? How to pass and return matrices in Java. Prerequisite:- Matrix in Java

matrix

Now let us see matrix programs in Java programming language. Read the problem, try it yourself, and then check the solution.


1) Java Program to Print 3×3 Matrix:- Write a Java program to display a 3×3 matrix. Take a matrix as input from the user and display it in various ways.

To print or display a 3×3 matrix you can use nested loops, it can be either for loop, for-each loop, while loop, or do-while loop. We have another better alternative deepToString() which is given in java.util.Arrays class. 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 an array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java.


2) Sum of Matrix Elements in Java:- Write a Java program to find the sum of matrix elements. Take a matrix, use the method to find the sum, and display the result.

Example:-

Matrix =
1 2 3
4 5 6
7 8 9
Sum of matrix elements = 45


3) Row Sum and Column Sum of a Matrix in Java:- Write a Java program to find the sum of each row and the sum of each column in a given matrix. Display the resultant values.

Example1:-

Matrix:
20 19 18
17 16 15
14 13 12
Row-1 sum = 57
Column-1 sum = 51
Row-2 sum = 48
Column-2 sum = 48
Row-3 sum = 39
Column-3 sum = 45

Example2:-

Matrix:
1 2 3
4 5 6
7 8 9
Row-1 sum = 6
Column-1 sum = 12
Row-2 sum = 15
Column-2 sum = 15
Row-3 sum = 24
Column-3 sum = 18


4) Sum of Diagonal Elements of Matrix in Java:- Write a Java Program to find the sum of diagonal elements of the matrix.

In a matrix the elements located at the position aij where i=j are called diagonal elements. For example, In the matrix “a” the elements located at positions a00, a11, a22 are diagonal elements. For example:-

Matrix =
1 2 3
4 5 6
7 8 9
Then the diagonal elements are:- 1, 5, 9
Sum of diagonal elements = 1+5+9 = 15


Matrix Programs on Matrix Operations (Addition, Subtraction, Transpose, and Multiplication)

5) Matrix Addition in Java:- Write a Java program to find the addition of two matrices. Take two matrices, declare a third matrix to store the result value, perform the operation, and display the result.

Let A =[aij] and B =[bij] be m × n matrices. The addition of two matrices 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].

The sum of two matrices of the same size is obtained by adding elements in the corresponding positions. 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.

Example:-

Matrix A:
1 2 3
4 5 6
7 8 9
Matrix B:
5 6 7
8 9 10
3 1 2
Sum (C):
6 8 10
12 14 16
10 9 11


6) Subtraction of Two Matrix in Java:- Write a Java program to find the subtraction of two matrices. Take two matrices, declare the third matrix to store the result value.

The subtraction of two matrices of the same size is obtained by subtracting elements in the corresponding positions. Condition for matrix subtraction:- Both matrices should have the same size.

Let A =[aij] and B =[bij] be m × n matrices. The subtraction 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]. Example:-

Matrix A:
20 19 18
17 16 15
14 13 12
Matrix B:
10 11 12
13 15 15
10 11 10
Subtraction (C):
10 8 6
4 1 0
4 2 2


7) Transpose of a Matrix in Java:- Write a Java program to find the transpose of a matrix. Take a matrix, find its transpose using method, and display the result.

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. Example:- for 3×2 Matrix,

Original matrix:-
a11 a12
a21 a22
a31 a32
Then the transpose of matrix:-
a11 a21 a31
a12 a22 a32

Example:-

Matrix:
1 2 3
4 5 6
7 8 9
Transpose =
1 4 7
2 5 8
3 6 9


8) Matrix Multiplication in Java:- Write Java Programs to find the multiplication of two matrices. Take two matrices, find the multiplication of them using the method, and display the result.

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. Example of Matrix Multiplication, 

Matrix A =
a11 a12
a21 a22

Matrix B =
b11 b12
b21 b22

The product of A and B is denoted as AB and it can be calculated as, AB=
a11*b11+a12*b21 a11*b12+a12*b22
a21*b11+a22*b21 a21*b12+a22*b22

Example using 2×2 matrices,

Matrix A =
1 3
7 5

Matrix B =
6 8
4 2

Multiplication =
18 14
62 66

Strassen’s had given another algorithm for finding the matrix multiplication. Unlike a simple divide and conquer method which uses 8 multiplications and 4 additions, Strassen’s algorithm uses 7 multiplications which reduces the time complexity of the matrix multiplication algorithm a little bit. See More:- Strassen’s Matrix Multiplication Algorithm


9) Menu Driven Program for Matrix Operations in Java:- Write Java programs for a menu-driven program for matrix operations. Perform matrix addition, subtraction, multiplication, and transpose using switch case. Use the help of the method.

Example:-

A = [[5, 6, 7], [8, 9, 10], [3, 1, 2]]
B = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Choose the matrix operation,
—————————-
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
—————————-
Enter your choice: 1
Sum of matrix:
[[6, 8, 10], [12, 14, 16], [10, 9, 11]]

Choose the matrix operation,
—————————-
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
—————————-
Enter your choice: 2
Subtraction of matrix:
[[4, 4, 4], [4, 4, 4], [-4, -7, -7]]

Choose the matrix operation,
—————————-
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
—————————-
Enter your choice: 3
Multiplication of matrix:
[[78, 96, 114], [114, 141, 168], [21, 27, 33]]

Choose the matrix operation,
—————————-
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
—————————-
Enter your choice: 4
Transpose of the first matrix:
[[5, 8, 3], [6, 9, 1], [7, 10, 2]]
Transpose of the second matrix:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Choose the matrix operation,
—————————-
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
—————————-
Enter your choice: 6
Invalid input.
Please enter the correct input.

Choose the matrix operation,
—————————-
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
—————————-
Enter your choice: 5
Thank You.

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