无法在 html 中显示矩阵

发布于 2025-01-20 04:01:26 字数 4682 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2025-01-27 04:01:26

我看不到任何HTML标签。也许这是部分代码库,因为也缺少GenerateMatrix的定义。您可以尝试此语法以打印矩阵,

    <!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A'</th>
    <?php
        // 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 -->
</table>
<!-- create element br -->
<br>

<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'B'</th>
    <?php 
        // 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 -->
</table>
<!-- create element br -->
<br>

<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A' + 'B'</th>
    <?php
        // 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 -->
</table>

<!-- create element br -->
<br>
<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A' * 'B'</th>
    <?php
        // 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 -->
</table>

“

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,

    <!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A'</th>
    <?php
        // 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 -->
</table>
<!-- create element br -->
<br>

<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'B'</th>
    <?php 
        // 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 -->
</table>
<!-- create element br -->
<br>

<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A' + 'B'</th>
    <?php
        // 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 -->
</table>

<!-- create element br -->
<br>
<!-- create element table and th -->
<table border='1' cellspacing='0' cellpadding='10'>
    <th colspan=4>Matrix 'A' * 'B'</th>
    <?php
        // 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 -->
</table>

Output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文