我正在尝试在 php 中制作矩阵乘法代码,但不是矩阵乘法,它只是与行和列的简单乘法

I am trying to make matrix multiplication code in php but instead of matrix multiplication it happens just simple multiplication with row and column

我想使用 PHP 中的数组进行矩阵乘法运算

这是我的代码:

<?php
$a1 = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));

$a2 = Array('0' => Array('0' => 7,'1' => 5),'1' => Array('0' => 3,'1' => 2));

$sumArray = array();

$result = array();
for($i=0; $i<=1; $i++)
{
    for($j=0; $j<=1; $j++)
    {
        $result[$i][$j] = $a1[$i][$j] * $a2[$i][$j];
    }
}
echo "<pre/>";
print_r($result);
?>

输出:

array image

矩阵乘法的实现方式如下:

for i = 1..N
   for j = 1..N
     result[i][j] = 0.
     for k = 1..N
       result[i][j] += array1[i][k] * array2[j][k] // "row times column"
     end for
   end for
end for 

希望我答对了你的问题。矩阵乘法需要 3 个 for 循环。

<?php



$a = Array('0' => Array('0' => 1,'1' => 2),'1' => Array('0' => 4,'1' => 5));


$b = Array('0' => Array('0' => 7,'1' => 5),'1' => Array('0' => 3,'1' => 2));

$sumArray = array();

$c = array();

for($i=0;$i<2;$i++) {
 for($j=0;$j<2;$j++) 
 { 
    $c[$i][$j]=0; 
    for($k=0;$k<2;$k++) 
        { $c[$i][$j]=$c[$i][$j]+($a[$i][$k]*$b[$k][$j]); 
    } 
} 
} 


echo "<pre/>";
print_r($c);
?>