PHP Program to subtraction of two matrices

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 a1 and a2 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[] to store the subtract of array a1[] and a2[]. Then assign the value 0 into the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in that we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to assign the calculated result of 'a1[i][j] - a2[i][j]' into the array sub[i][j] and at last print the elements of the two-dimensional array sub[][] using for loop

ALGORITHM

Step 1: Initialize 2 two-dimensional arrays a1[] and a2[]

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 a1[] and a2[]

Step 4: Create an empty array sub[] to store the subtract of array a1[] and a2[]

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

        (i) 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

            (a) Assign the calculated result of 'a1[i][j] - a2[i][j]' into the array sub[i][j]

Step 6: Print the elements of the two-dimensional array sub[][] using for loop

PHP Source Code

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