[英]I am trying to make matrix multiplication code in php but instead of matrix multiplication it happens just simple multiplication with row and column
I want to do matrix multiplication using array in PHP I have trying to do same but instead of matrix multiplication it just happening Simple multiplication as per output please help me to resolve it. 我想在PHP中使用数组来做矩阵乘法,但我尝试做同样的事情,但不是矩阵乘法,而是按输出进行简单乘法,请帮助我解决它。
Here is my code: 这是我的代码:
<?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);
?>
Output: 输出:
<?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);
?>
matrix multiplication is implemented the following way: 矩阵乘法通过以下方式实现:
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
I hope I got your question right. 希望我能正确回答你的问题。 Matrix-Multiplication requires 3 for-loops.
矩阵乘法需要3个for循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.