PHP Program to subtraction of two matrices


February 15, 2022, Learn eTutorial
1441

It is an array based PHP program. A matrix is a two-dimensional array and subtraction is an operation done between 2 matrices.

NOTE: To do subtraction between matrices the order of both matrices should be the same.

We require knowledge of the following topics to understand the program clearly. So, let's have a look at them.

How to find the subtraction of two matrices?

To find the subtraction of 2 two-dimensional arrays first we have to find the number of rows and columns in the array and subtract the corresponding index together. For example, If the arrays are

        2  5  8              1  3  1

a[] = 6  2  5     b[] =  3  1  1

        4  5  6               3  1  2

then the result will be:

1  2  7

3  1  4

1  4  4

How to find the subtraction of two matrices using PHP?

In this program, we are using the static values which are initialized in the code itself. First, we have to initialize 2 two-dimensional arrays arr1 and arr2, we have to assign the number of rows and columns into the variable row and col using the built-in function count(). Then we have to create an empty array sub_arr[ ] to store the subtracted value of array arr1[ ] and arr2[ ]. Then using a nested for loop, until the condition 'i < row' becomes false in 1st loop and until the condition 'j < col' becomes false in 2nd loop assign the calculated difference of arr1[i][j] - arr2[i][j] into the array sub_arr[i][j]. After subtraction print the elements of the two-dimensional array sub_arr[ ][ ] using for loop

ALGORITHM

STEP 1: Initialize 2 two-dimensional arrays arr1[ ] and arr2[ ]

STEP 2: Assign the number of rows and columns into the variable row and col using the built-in function count()

STEP 3: Print the elements in the array arr1[ ] and arr2 []

STEP 4: Create an empty array sub_arr[ ] to store the subtract of array arr1[ ] and arr2[ ]

STEP 5: Assign the value 0 into the variable i and perform the step 6 until the condition 'i < row' becomes false and increment the value of variable i in every iteration

STEP 6: Assign the value 0 into the variable j and perform the sub-step until the condition 'j < col' becomes false and increment the value of variable j in every iteration

  • Assign the calculated result of 'arr1[i][j] - arr2[i][j]' into the array sub_arr[i][j]

Step 7: Print the elements of the two-dimensional array sub_arr[ ][ ] using for loop

PHP Source Code

                                          <?php
$arr1 = array(
    array(6, 7, 7),
    array(9, 5, 2),
    array(8, 4, 8)
);
$arr2 = array(
    array(4, 6, 5),
    array(3, 2, 1),
    array(6, 3, 3)
);
$row = count($arr1);
$col = count($arr1[0]);
echo "First matrix: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $arr1[$i][$j] . " ";
    }
    echo "\n";
}
echo "Second matrix: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $arr2[$i][$j] . " ";
    }
    echo "\n";
}
$sub_arr = array();
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        $sub_arr[$i][$j] = $arr1[$i][$j] - $arr2[$i][$j];
    }
}
echo "Subtraction of two matrices: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $sub_arr[$i][$j] . " ";
    }
    echo "\n";
}
?>

                                      

OUTPUT

First matrix:
6 7 7
9 5 2
8 4 8
Second matrix:
4 6 5
3 2 1
6 3 3
Subtraction of two matrices:
2 1 2
6 3 1
2 1 5