- Matrix programing in java
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Matrix Programs in Java
- Matrix Programs on Matrix Operations (Addition, Subtraction, Transpose, and Multiplication)
- Matrix Programs in Java
- Graphical Representation of Matrix
- Matrix in Java
- Matrix Programs in Java
- 1. Adding Two Matrix
- 2. Subtracting Two Matrices
- 3. Multiplying Two Matrices
Matrix programing in java
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
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 i th row and j th 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
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.
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.
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
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.
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 A t , is the n × m matrix obtained by interchanging the rows and columns of A. In other words, if A t =[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
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,
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,
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.
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 do you find anything incorrect? Let us know in the comments. Thank you!
Matrix Programs in Java
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
A Matrix is a rectangular array. The elements are arranged in the rows and columns. In this tutorial, we will look at some matrix programs in Java.
Graphical Representation of Matrix
Matrix in Java
We can implement a matrix using two dimensional array in Java. The element at row “r” and column “c” can be accessed using index “array[r][c]”.
Matrix Programs in Java
Since we are using two-dimensional arrays to create a matrix, we can easily perform various operations on its elements. In this tutorial, we will learn how to create a matrix from user input. Then we will add, subtract, and multiply two matrices and print the result matrix on the console.
1. Adding Two Matrix
Here is the simple program to populate two matrices from the user input. Then add its elements at the corresponding indices to get the addition of the matrices. Finally, we will print the sum of the matrices.
package com.journaldev.examples; import java.util.Scanner; public class MatrixPrograms < public static void main(String[] args) < System.out.println("Please enter the rows in the matrix"); Scanner sc = new Scanner(System.in); int row = sc.nextInt(); System.out.println("Please enter the columns in the matrix"); int column = sc.nextInt(); int[][] first = new int[row][column]; int[][] second = new int[row][column]; for (int r = 0; r < row; r++) < for (int c = 0; c < column; c++) < System.out.println(String.format("Enter first[%d][%d] integer", r, c)); first[r][c] = sc.nextInt(); >> for (int r = 0; r < row; r++) < for (int c = 0; c < column; c++) < System.out.println(String.format("Enter second[%d][%d] integer", r, c)); second[r][c] = sc.nextInt(); >> // close the scanner sc.close(); // print both matrices System.out.println("First Matrix:\n"); print2dArray(first); System.out.println("Second Matrix:\n"); print2dArray(second); // sum of matrices sum(first, second); > // below code doesn't take care of exceptions private static void sum(int[][] first, int[][] second) < int row = first.length; int column = first[0].length; int[][] sum = new int[row][column]; for (int r = 0; r < row; r++) < for (int c = 0; c < column; c++) < sum[r][c] = first[r][c] + second[r][c]; >> System.out.println("\nSum of Matrices:\n"); print2dArray(sum); > private static void print2dArray(int[][] matrix) < for (int r = 0; r < matrix.length; r++) < for (int c = 0; c < matrix[0].length; c++) < System.out.print(matrix[r][c] + "\t"); >System.out.println(); > > >
2. Subtracting Two Matrices
Here is the function to subtraction second matrix elements from the first matrix and then print the result matrix.
private static void subtract(int[][] first, int[][] second) < int row = first.length; int column = first[0].length; int[][] sum = new int[row][column]; for (int r = 0; r < row; r++) < for (int c = 0; c < column; c++) < sum[r][c] = first[r][c] - second[r][c]; >> System.out.println("\nSubtraction of Matrices:\n"); print2dArray(sum); >
3. Multiplying Two Matrices
private static void multiply(int[][] first, int[][] second) < int row = first.length; int column = first[0].length; int[][] sum = new int[row][column]; for (int r = 0; r < row; r++) < for (int c = 0; c < column; c++) < sum[r][c] = first[r][c] * second[r][c]; >> System.out.println("\nMultiplication of Matrices:\n"); print2dArray(sum); >
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us