PHP Program to find Matrix Multiplication
You are Here:
Find Matrix Multiplication
In the following example, we will multiply the two given matrices (two-dimensional arrays).
Example
PHP Compiler
<?php
$arr1 = array(
array(1, 1, 1),
array(1, 1, 1),
array(1, 1, 1),
);
$arr2 = array(
array(2, 2, 2),
array(2, 2, 2),
array(2, 2, 2),
);
$arr3 = array(
array(0, 0, 0),
array(0, 0, 0),
array(0, 0, 0),
);
echo "Matrix A (3 x 3): <br>";
for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
echo $arr1[$i][$j] . " ";
echo "<br>";
}
echo "<br>Matrix B (3 x 3): <br>";
for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
echo $arr2[$i][$j] . " ";
echo "<br>";
}
for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
{
for($k=0; $k<3; $k++)
$arr3[$i][$j] = $arr3[$i][$j] + $arr1[$i][$k] * $arr2[$k][$j];
}
}
echo "<br>Matrix Multiplication (A x B): <br>";
for($i=0; $i<3; $i++)
{
for($j=0; $j<3; $j++)
echo $arr3[$i][$j] . " ";
echo "<br>";
}
?>
Output
Matrix A (3 x 3):
1 1 1
1 1 1
1 1 1
Matrix B (3 x 3):
2 2 2
2 2 2
2 2 2
Matrix Multiplication (A x B):
6 6 6
6 6 6
6 6 6
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on PHP with examples for quick and easy learning.
We are working to cover every Single Concept in PHP.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.