DEV Community

Daisy Rono
Daisy Rono

Posted on • Edited on

Splitting a 3x3 matrix into all possible 2x2 matrices using PHP

Creating the 3x3 matrix

To create a 3x3 matrix, begin with an array

$a = array(array(1,5,7),array(7,3,5),array(2,6,9));
$rows = count($a);
$cols = count($a[0]);

for ($k = 0; $k < $rows; $k++){
    for ($j = 0; $j < $cols; $j++){
        echo ($a[$k][$j]."  ");
    }
    echo ("<br>");
}
Enter fullscreen mode Exit fullscreen mode

This will result in

1 5 7
7 3 5
2 6 9
Enter fullscreen mode Exit fullscreen mode

Getting 2x2 matrices

There are 4 possible 2x2 matrices that could be derived from the 3x3 matrix above.

  • Begin with row index zero and column index zero and reduce the number of rows and columns by one. (since its a 3x3 matrix we wish to reduce to a 2x2, the row and column both reduce by one)

  • To create another matrix, Increment either the row by one. Repeat this step with the column, this time holding the row constant.

The first 2x2 matrix is begins at k = 0 and j = 0. However, the arrays are reduced by one from both ends like seen in the code:

for ($k = 0; $k < $rows-1; $k++){
    for ($j = 0; $j < $cols-1; $j++){
        echo ($a[$k][$j]." ");
    }
    echo ("<br>");
}
Enter fullscreen mode Exit fullscreen mode

The output of this is:

1 5
7 3
Enter fullscreen mode Exit fullscreen mode

The maximum and minimum values of this matrix are 7 and 1 respectively.

The second 2x2 matrix has the row starting at k=0 and ending at k < row-1 but the column starts at j =1 and ends at j < cols as shown below:

for ($k = 0; $k < $rows-1; $k++){
    for ($j = 1; $j < $cols; $j++){
        echo ($a[$k][$j]." ");
    }
    echo ("<br>");
}
Enter fullscreen mode Exit fullscreen mode

The output of this is:

5 7
3 5
Enter fullscreen mode Exit fullscreen mode

The maximum and minimum values of this matrix are 7 and 3 respectively.

Here, the rows start at k =1 while the columns start at j =0.

for ($k = 1; $k < $rows; $k++){
    for ($j = 0; $j < $cols-1; $j++){
        echo ($a[$k][$j]." ");
    }
    echo ("<br>");
}
Enter fullscreen mode Exit fullscreen mode

The output of this is:

7 3
2 6
Enter fullscreen mode Exit fullscreen mode

The maximum and minimum values of this matrix are 7 and 2 respectively.

Now, both rows and columns start at 1.

for ($k = 1; $k < $rows; $k++){
    for ($j = 1; $j < $cols; $j++){
        echo ($a[$k][$j]." ");
    }
    echo ("<br>");
}

Enter fullscreen mode Exit fullscreen mode

The output of this is:

3 5
6 9

Enter fullscreen mode Exit fullscreen mode

The maximum and minimum values of this matrix are 9 and 3 respectively.

The Minimum and Maximum values Matrices

To merging the minimum values to form other matrix of minimum values using arrays, use

#minimum value
echo "<br>";
$b =array(array(1,3),array(2,3));
$rows_b = count($b);
$cols_b = count($b[0]);
for ($i = 0; $i < $rows_b; $i++){
    for ($l = 0; $l < $cols_b; $l++){
        echo ($b[$i][$l]."  ");
    }
    echo ("<br>");
}
Enter fullscreen mode Exit fullscreen mode

This results to a matrix of the form

1 3
2 3

Enter fullscreen mode Exit fullscreen mode

The same concept is applied to maximum values using

$c =array(array(7,7),array(7,9));
$rows_c = count($c);
$cols_c = count($c[0]);
for ($m = 0; $m < $rows_c; $m++){
    for ($n = 0; $n < $cols_c; $n++){
        echo ($c[$m][$n]."  ");
    }
    echo ("<br>");
}
Enter fullscreen mode Exit fullscreen mode

resulting in the 2x2 matrix below.

7 7
7 9

Enter fullscreen mode Exit fullscreen mode

Top comments (0)