无法在 html 中显示矩阵
我有一个 php 代码,但不知道如何在 HTML 表中显示它。我创建了矩阵表来进行加法和乘法,但不知道如何将结果放入 HTML 表中。我尝试将代码放在脚本标记之间,但它不起作用。我的矩阵函数基本上生成带有随机整数的 4 x 4 矩阵,并生成两个名为 A 和 B 的矩阵。然后我创建了另外两个函数来对这些矩阵进行加法和乘法,但我没有将它们打印在 HTML 表中。 我的代码:
// creating function addMatrix which takes two numbers and returns a new matrix
function addMatrix($A, $B)
{
// declaring $ADD as a empty array
$ADD = array();
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// adding $A[$i][$j] to $B[$i][$j] and store the result in $ADD[$i][$j]
$ADD[$i][$j] = $A[$i][$j] + $B[$i][$j];
}
}
// return $ADD
return $ADD;
}
// creating function mulMatrix which takes two numbers and returns a new matrix
function mulMatrix($A, $B)
{
// declaring $MUL as a empty array
$MUL = array();
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// assign 0 in $MUL[$i][]
$MUL[$i][] = 0;
// iterate from k is equal to 0, k is less than 4 and increment k by 1
for ($k = 0; $k < 4; $k++) {
// multiply $ADD[$i][$k] with $B[$k][$j] then add it in $MUL[$i][$j] and store it
// in $MUL[$i][$j]
$MUL[$i][$j] += $A[$i][$k] * $B[$k][$j];
}
}
}
// return $MUL
return $MUL;
}
// generate matrix by calling generateMatrix and store it in $A
$A = generateMatrix();
// generate matrix by calling generateMatrix and store it in $B
$B = generateMatrix();
// add matrix by calling addMatrix and store it in $ADD
$ADD = addMatrix($A, $B);
// mul matrix by calling mulMatrix and store it in $MUL
$MUL = mulMatrix($A, $B);
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $A[$i][$j] with element td
echo "<td>" . $A[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tag table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'B'</th>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $A[$i][$j] with element td
echo "<td>" . $B[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' + 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $ADD[$i][$j] with element td
echo "<td>" . $ADD[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' * 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $MUL[$i][$j] with element td
echo "<td>" . $MUL[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";here
I have a php code but don't know how to show it in HTML table. I created matrix tables to add and multiply but don't know how to put results in HTML table. I tried to put the code between script tags but it didn't work. My matrix func basically generates 4 by 4 matrices with random integers and generates two matrices called A and B. then I created two other functions to add and multiply these matrices but I didn't print them out in HTML table.
My code:
// creating function addMatrix which takes two numbers and returns a new matrix
function addMatrix($A, $B)
{
// declaring $ADD as a empty array
$ADD = array();
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// adding $A[$i][$j] to $B[$i][$j] and store the result in $ADD[$i][$j]
$ADD[$i][$j] = $A[$i][$j] + $B[$i][$j];
}
}
// return $ADD
return $ADD;
}
// creating function mulMatrix which takes two numbers and returns a new matrix
function mulMatrix($A, $B)
{
// declaring $MUL as a empty array
$MUL = array();
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// assign 0 in $MUL[$i][]
$MUL[$i][] = 0;
// iterate from k is equal to 0, k is less than 4 and increment k by 1
for ($k = 0; $k < 4; $k++) {
// multiply $ADD[$i][$k] with $B[$k][$j] then add it in $MUL[$i][$j] and store it
// in $MUL[$i][$j]
$MUL[$i][$j] += $A[$i][$k] * $B[$k][$j];
}
}
}
// return $MUL
return $MUL;
}
// generate matrix by calling generateMatrix and store it in $A
$A = generateMatrix();
// generate matrix by calling generateMatrix and store it in $B
$B = generateMatrix();
// add matrix by calling addMatrix and store it in $ADD
$ADD = addMatrix($A, $B);
// mul matrix by calling mulMatrix and store it in $MUL
$MUL = mulMatrix($A, $B);
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $A[$i][$j] with element td
echo "<td>" . $A[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tag table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'B'</th>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $A[$i][$j] with element td
echo "<td>" . $B[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' + 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $ADD[$i][$j] with element td
echo "<td>" . $ADD[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' * 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $MUL[$i][$j] with element td
echo "<td>" . $MUL[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不到任何HTML标签。也许这是部分代码库,因为也缺少GenerateMatrix的定义。您可以尝试此语法以打印矩阵,
I can't see any HTML tag. Maybe it's a partial codebase because the definition of generateMatrix is also missing. You can try this syntax to print the matrix,