PHP Basic
PHP Interview Programs
- PHP Interview Programs
- PHP Abundant Number
- PHP Amicable Number
- PHP Armstrong Number
- PHP Average of N Numbers
- PHP Automorphic Number
- PHP Biggest of three numbers
- PHP Binary to Decimal
- PHP Common Divisors
- PHP Composite Number
- PHP Condense a Number
- PHP Cube Number
- PHP Decimal to Binary
- PHP Decimal to Octal
- PHP Disarium Number
- PHP Even Number
- PHP Evil Number
- PHP Factorial of a Number
- PHP Fibonacci Series
- PHP GCD
- PHP Happy Number
- PHP Harshad Number
- PHP LCM
- PHP Leap Year
- PHP Magic Number
- PHP Matrix Addition
- PHP Matrix Division
- PHP Matrix Multiplication
- PHP Matrix Subtraction
- PHP Matrix Transpose
- PHP Maximum Value of an Array
- PHP Minimum Value of an Array
- PHP Multiplication Table
- PHP Natural Number
- PHP Number Combination
- PHP Odd Number
- PHP Palindrome Number
- PHP Pascalβs Triangle
- PHP Perfect Number
- PHP Perfect Square
- PHP Power of 2
- PHP Power of 3
- PHP Pronic Number
- PHP Prime Factor
- PHP Prime Number
- PHP Smith Number
- PHP Strong Number
- PHP Sum of Array
- PHP Sum of Digits
- PHP Swap Two Numbers
- PHP Triangular Number
PHP 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 php program that performs matrix subtraction.
In the provided php 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 php code that performs matrix subtraction.
<?php
// Function to perform matrix subtraction
function subtractMatrices($mat1, $mat2)
{
$result = array();
for ($i = 0;$i < 3;++$i)
{
for ($j = 0;$j < 3;++$j)
{
$result[$i][$j] = $mat1[$i][$j] - $mat2[$i][$j];
}
}
return $result;
}
// Function to display a matrix
function displayMatrix($matrix)
{
for ($i = 0;$i < 3;++$i)
{
for ($j = 0;$j < 3;++$j)
{
echo $matrix[$i][$j] . "\t";
}
echo "\n";
}
}
// Sample matrices for subtraction
$matrix1 = array(
array(5, 8, 2),
array(7, 4, 9),
array(3, 6, 1)
);
$matrix2 = array(
array(3, 1, 7),
array(6, 9, 2),
array(8, 5, 4)
);
// Perform matrix subtraction
$resultMatrix = subtractMatrices($matrix1, $matrix2);
// Display matrices and result
echo "Matrix 1:\n";
displayMatrix($matrix1);
echo "\nMatrix 2:\n";
displayMatrix($matrix2);
echo "\nResultant Matrix (Matrix1 - Matrix2):\n";
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
Run the program to see the result of the matrix subtraction.
π§ How the Program Works
- The program defines a function subtractMatrices that takes two 3x3 matrices as input and calculates their subtraction, returning the result as a new matrix.
- It also defines a function displayMatrix to print a matrix.
- Sample matrices $matrix1 and $matrix2 are defined, and matrix subtraction is performed using the subtractMatrices function.
- The result is displayed using the displayMatrix function.
π§ 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 (PHP Program to Perform Matrix Subtraction), please comment here. I will help you immediately.