C++程序查找给定矩阵的迹线和法线

一些应用从二维数组或矩阵的使用中受益匪浅。 数字存储在矩阵的行和列中。使用多维数组,我们 也可以在C++中定义 2D 矩阵。在这篇文章中,我们将看看如何使用C++ 确定给定矩阵的法线和迹线。

矩阵中元素总数的平方根就是所谓的 正常。轨迹由构成主对角线的所有组件组成。让我们 查看算法在C++代码中的表示形式。

矩阵跟踪

主对角线中所有元素的总和:(8 + 7 + 9) = 24,这是给定矩阵的迹线

在前面的示例中,使用了一个 3 x 3 矩阵,结果是 主要对角线中的元素数。矩阵的轨迹可以在总和中找到。让我们 看看算法来帮助我们理解。

算法

  • 读取矩阵 M 作为输入
  • 假设 M 有 n 行和 n 列
  • 总和 := 0
  • 对于范围从 1 到 n的 i,请执行
    • 总和 := 总和 + M[ i ][ i ]
  • 结束
  • 返回总和

Example



#include <iostream> #include <cmath> #define N 7 using namespace std ; float solve ( int M [ N ] [ N ] ) { int sum = 0 ; // read elements through major diagonal, where row index and column index are same, both are i for ( int i = 0 ; i < N ; i ++ ) { sum = sum + M [ i ] [ i ] ; } return sum ; } int main ( ) { int mat1 [ N ] [ N ] = { { 5 , 8 , 74 , 21 , 69 , 78 , 25 } , { 48 , 2 , 98 , 6 , 63 , 52 , 3 } , { 85 , 12 , 10 , 6 , 9 , 47 , 21 } , { 6 , 12 , 18 , 32 , 5 , 10 , 32 } , { 8 , 45 , 74 , 69 , 1 , 14 , 56 } , { 7 , 69 , 17 , 25 , 89 , 23 , 47 } , { 98 , 23 , 15 , 20 , 63 , 21 , 56 } , } ; cout << "The Trace of the first matrix is: " << solve ( mat1 ) << endl ; int mat2 [ N ] [ N ] = { { 6 , 8 , 35 , 21 , 87 , 8 , 26 } , { 99 , 2 , 36 , 326 , 25 , 24 , 56 } , { 15 , 215 , 3 , 157 , 8 , 41 , 23 } , { 96 , 115 , 17 , 5 , 3 , 10 , 18 } , { 56 , 4 , 78 , 5 , 10 , 22 , 58 } , { 85 , 41 , 29 , 65 , 47 , 36 , 78 } , { 12 , 23 , 87 , 45 , 69 , 96 , 12 } } ; cout << "The Trace of the second matrix is: " << solve ( mat2 ) << endl ; }

输出

<span style="color:#000000">The Trace of the first matrix is: 129
The Trace of the second matrix is: 74
</span>

矩阵法线

所有元素的总和:(8 + 5 + 3 + 6 + 7 + 1 + 2 + 4 + 9) = 45

正态:(所有元素之和的平方根)= √45 = 6.708

在最后一个示例中,使用了 3 x 3 矩阵。我们首先计算了其所有条目的总和 在取其平方根之前。让我们看看帮助我们理解的算法。

算法

  • 读取矩阵 M 作为输入
  • 假设 M 有 n 行和 n 列
  • 总和初始化为 0
  • 对于范围从 1 到 n的 i,请执行
    • 对于范围从 1 到 n 的 J,请执行
      • 总和 := 总和 + M[ i ][ j ]
    • 结束
  • 结束
  • res := 和的平方根
  • 返回分辨率



#include <iostream> #include <cmath> #define N 7 using namespace std ; float solve ( int M [ N ] [ N ] ) { int sum = 0 ; // go through each element. Using outer loop, access ith row, using inner loop access column. For cell (i, j) read the element and add it to the sum for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { sum = sum + M [ i ] [ j ] ; } } return sqrt ( sum ) ; } int main ( ) { int mat1 [ N ] [ N ] = { { 5 , 8 , 74 , 21 , 69 , 78 , 25 } , { 48 , 2 , 98 , 6 , 63 , 52 , 3 } , { 85 , 12 , 10 , 6 , 9 , 47 , 21 } , { 6 , 12 , 18 , 32 , 5 , 10 , 32 } , { 8 , 45 , 74 , 69 , 1 , 14 , 56 } , { 7 , 69 , 17 , 25 , 89 , 23 , 47 } , { 98 , 23 , 15 , 20 , 63 , 21 , 56 } , } ; cout << "The Normal of the first matrix is: " << solve ( mat1 ) << endl ; int mat2 [ N ] [ N ] = { { 6 , 8 , 35 , 21 , 87 , 8 , 26 } , { 99 , 2 , 36 , 326 , 25 , 24 , 56 } , { 15 , 215 , 3 , 157 , 8 , 41 , 23 } , { 96 , 115 , 17 , 5 , 3 , 10 , 18 } , { 56 , 4 , 78 , 5 , 10 , 22 , 58 } , { 85 , 41 , 29 , 65 , 47 , 36 , 78 } , { 12 , 23 , 87 , 45 , 69 , 96 , 12 } } ; cout << "The Normal of the second matrix is: " << solve ( mat2 ) << endl ; }

输出

<span style="color:#000000">The Normal of the first matrix is: 41.1947
The Normal of the second matrix is: 49.4267
</span>

结论

正常和跟踪操作属于矩阵。这两个过程需要 方阵,这就是我们需要的(对于跟踪平方是需要的)。跟踪是 矩阵的主对角线中包含的元素,而法线只是 矩阵中包含的元素总数的平方根。在C++中,矩阵可以 使用 2D 数组显示。在这里,我们选择了两个矩阵,5行5列 作为示例(总共 25 个元素)。必须通过循环语句访问矩阵 和索引操作。需要两个嵌套循环,因为要执行典型的 计算,我们必须遍历每个元素。而这个程序的复杂度是O(n2). 由于跟踪只需要主要对角线,因此行索引和列索引将是 相同。因此,只需要一个 for 循环。在O(n)时间内,它是确定的。

posted @ 2022-12-17 20:40  很酷的站长  阅读(26)  评论(0编辑  收藏  举报
70博客 AI工具 源码下载