Multiplication of Two Matrix in PHP of Same Dimensions using For loop
ADVERTISEMENTS
Multiplication of two matrix in PHP of same dimensions using for loop. In this article, you will learn how to make multiplication of two matrix in php of same dimensions using for loop.
Matrix Multiplication Formula
{ M1 } x { M2 } = { M3 }
Source Code
<?php
// Multiplication of Two Matrix in PHP of Same Dimensions using For loop
$x = array (
array (32, 54, 65),
array (45, 54, 65),
array (45, 32, 65),
array (45, 32, 54)
);
$y = array (
array (54, 24, 76),
array (90, 24, 76),
array (90, 54, 76),
array (90, 54, 24)
);
// $x & $y - To store the two matrix elements of 4 x 3 dimensions
$m = array ();
// $m - To store multiplication of matrices
echo "-----The multiplication of the matrices is-----\n\n";
// It's the calculation of matrices's multiplications
for ($i = 0; $i < count ($x); $i++) {
$m[] = array ();
for ($j = 0; $j < count ($x[$i]); $j++) {
$m[$i][] = 0;
for ($k = 0; $k < count ($x[$i]); $k++) {
$m[$i][$j] += $x[$i][$k] * $y[$k][$j];
}
}
}
// This will display matrices' outputs
for ($i = 0; $i < count ($x); $i++) {
echo "\t";
for ($j = 0; $j < count ($x[$i]); $j++) {
echo $m[$i][$j]."\t";
}
echo "\n";
}
?>
Output
-----The multiplication of the matrices is-----
12438 5574 11476
13140 5886 12464
11160 5358 10792
10170 4764 9956