Java Basic
Java Interview Programs
- Java Interview Programs
- Java Abundant Number
- Java Amicable Number
- Java Armstrong Number
- Java Average of N Numbers
- Java Automorphic Number
- Java Biggest of three numbers
- Java Binary to Decimal
- Java Common Divisors
- Java Composite Number
- Java Condense a Number
- Java Cube Number
- Java Decimal to Binary
- Java Decimal to Octal
- Java Disarium Number
- Java Even Number
- Java Evil Number
- Java Factorial of a Number
- Java Fibonacci Series
- Java GCD
- Java Happy Number
- Java Harshad Number
- Java LCM
- Java Leap Year
- Java Magic Number
- Java Matrix Addition
- Java Matrix Division
- Java Matrix Multiplication
- Java Matrix Subtraction
- JS Matrix Transpose
- Java Maximum Value of an Array
- Java Minimum Value of an Array
- Java Multiplication Table
- Java Natural Number
- Java Number Combination
- Java Odd Number
- Java Palindrome Number
- Java Pascalβs Triangle
- Java Perfect Number
- Java Perfect Square
- Java Power of 2
- Java Power of 3
- Java Pronic Number
- Java Prime Factor
- Java Prime Number
- Java Smith Number
- Java Strong Number
- Java Sum of Array
- Java Sum of Digits
- Java Swap Two Numbers
- Java Triangular Number
Java Program to Perform Matrix Subtraction
Photo Credit to CodeToFun
π Introduction
Matrix subtraction is a fundamental operation in linear algebra and computer science.
Matrix subtraction involves subtracting each element of one matrix from the corresponding element of another matrix of the same order.
In this tutorial, we'll explore a Java program that performs matrix subtraction.
In the provided Java program, we'll perform matrix subtraction for 3x3 matrices as an example. You can easily modify the program to work with matrices of different sizes.
π Example
Let's dive into the Java code that performs matrix subtraction.
public class MatrixSubtraction {
// Function to perform matrix subtraction
static void subtractMatrices(int[][] mat1, int[][] mat2, int[][] result) {
for (int i = 0; i < mat1.length; ++i) {
for (int j = 0; j < mat1[0].length; ++j) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
// Function to display a matrix
static void displayMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; ++i) {
for (int j = 0; j < matrix[0].length; ++j) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
// Driver program
public static void main(String[] args) {
// Sample matrices for subtraction
int[][] matrix1 = {
{5, 8, 2},
{7, 4, 9},
{3, 6, 1}
};
int[][] matrix2 = {
{3, 1, 7},
{6, 9, 2},
{8, 5, 4}
};
int[][] resultMatrix = new int[3][3];
// Perform matrix subtraction
subtractMatrices(matrix1, matrix2, resultMatrix);
// Display matrices and result
System.out.println("Matrix 1:");
displayMatrix(matrix1);
System.out.println("\nMatrix 2:");
displayMatrix(matrix2);
System.out.println("\nResultant Matrix (Matrix1 - Matrix2):");
displayMatrix(resultMatrix);
}
}
π» Testing the Program
Feel free to replace the sample matrices with your own 3x3 matrices in the main function to test the program with different input.
Matrix 1: 5 8 2 7 4 9 3 6 1 Matrix 2: 3 1 7 6 9 2 8 5 4 Resultant Matrix (Matrix1 - Matrix2): 2 7 -5 1 -5 7 -5 1 -3
Compile and run the program to see the result of the matrix subtraction.
π§ How the Program Works
- The program defines a class MatrixSubtraction containing a static method subtractMatrices that takes two matrices as input and calculates their subtraction, storing the result in another matrix.
- It also contains a static method displayMatrix to print a matrix.
- In the main method, sample matrices matrix1 and matrix2 are defined, and matrix subtraction is performed using the subtractMatrices method.
- The result is displayed using the displayMatrix method.
π§ Understanding the Concept of Matrix Subtraction
Matrix subtraction is performed element-wise, subtracting the corresponding elements of one matrix from the elements of another matrix. The result is a new matrix of the same order.
In the provided example, the element in the first row and first column of the result matrix is obtained by subtracting the element in the first row and first column of Matrix 2 from the element in the first row and first column of Matrix 1, and so on.
π Conclusion
Matrix subtraction is a fundamental operation in linear algebra and computer science. This C program provides a practical implementation of matrix subtraction and serves as a foundation for more advanced matrix operations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Java Program to Perform Matrix Subtraction), please comment here. I will help you immediately.