Golang 使用多维数组乘以矩阵

Golang 使用多维数组乘以矩阵

在本教程中,我们将编写一个Go语言程序来乘以两个矩阵。单维数组和多维数组的区别在于前者在索引上保存了一个属性,而后者在索引上保存了另一个数组。此外,多维数组的每个元素将具有相同的数据类型。

方法1:使用主函数中的多维数组相乘两个矩阵

在这种方法中,我们将编写一个Go语言程序,在main()函数中使用for循环来相乘两个多维矩阵。

步骤

步骤1: 导入fmt包。

步骤2: 现在,开始main()函数。初始化两个整数类型的矩阵并将值存储到它们中。进一步,将这些矩阵打印在屏幕上。

步骤3: 为了相乘矩阵,使用三个for循环。在每次迭代矩阵时,通过将两个矩阵的行与列相乘并相加,更新总变量。

步骤4: 在更新总变量后,将结果存储在相应位置的结果变量中,将总变量重新初始化为零,并重复该过程。

步骤5: 使用fmt.Println()函数将得到的最终结果打印到屏幕上。

示例

使用多维数组乘以两个矩阵的Go语言程序。

package main
import "fmt"
func main() {

   // initializing variables
   var result [3][2]int
   var i, j, k, total int
   total = 0
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   matrixB := [3][2]int{
      {10, 11},
      {13, 14},
      {16, 17},
   }

   // printing matrices on the screen
   fmt.Println("The first matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }

   // printing a new line
   fmt.Println()
   fmt.Println("The second matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 2; j++ {
         fmt.Print(matrixB[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()

   // multiplying matrices and storing result
   for i = 0; i < 3; i++ {
      for j = 0; j < 2; j++ {
         for k = 0; k < 3; k++ {
            total = total + matrixA[i][k]*matrixB[k][j]
         }
         result[i][j] = total
         total = 0
      }
   }

   // printing result on the screen
   fmt.Println("Results of matrix multiplication: ")
   for i = 0; i < 3; i++ {
      for j = 0; j < 2; j++ {
         fmt.Print(result[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
}

输出

The first matrix is:
0  1  2
4  5  6
8  9 10
The second matrix is:
10  11
13  14
16  17
Results of matrix multiplication:
45    48
201  216
357  384

方法2:使用多维数组在外部函数中相乘两个矩阵

在这种方法中,我们将创建一个用户定义的函数来执行两个矩阵的乘法过程。我们创建的函数将把相应的矩阵作为参数,并在执行乘法后返回最终的矩阵,我们可以接收并打印在屏幕上。

步骤

步骤1 - 导入fmt包。

步骤2 - 创建一个函数MultiplyMatrix()来相乘给定的矩阵。这个函数接受两个矩阵作为参数,并将最终的矩阵作为结果返回。

步骤3 - 这个函数使用三个for循环来实现逻辑。在矩阵的每次迭代中,我们通过将两个矩阵的行与列相乘并相加来更新total变量。

步骤4 - 在更新total变量后,将结果存储在相应的位置的result变量中,重新初始化total为零,并重复这个过程。

步骤5 - 完成所有的迭代后返回结果。

步骤6 - 现在,开始main()函数。初始化两个整数类型的矩阵,并给它们赋值。进一步,在屏幕上打印这些矩阵。

步骤7 - 通过将两个矩阵作为参数传递给MultiplyMatrix()函数并存储结果来调用MultiplyMatrix()函数。

步骤8 - 使用fmt.Println()函数在屏幕上打印最终结果。

示例

使用多维数组通过外部函数相乘两个矩阵的Golang程序

package main
import (
   "fmt"
)

// creating a function to multiply matrices
func MultiplyMatrix(matrixA [3][3]int, matrixB [3][2]int) [3][2]int {
   var total int = 0
   var result [3][2]int

   // multiplying matrices and storing result
   for i := 0; i < 3; i++ {
      for j := 0; j < 2; j++ {
         for k := 0; k < 3; k++ {
            total = total + matrixA[i][k]*matrixB[k][j]
         }
         result[i][j] = total
         total = 0
      }
   }
   return result
}
func main() {

   // initializing variables
   var result [3][2]int
   var i, j int
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   matrixB := [3][2]int{
      {10, 11},
      {13, 14},
      {16, 17},
   }

   // printing matrices on the screen
   fmt.Println("The first matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }

   // printing a new line
   fmt.Println()
   fmt.Println("The second matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 2; j++ {
         fmt.Print(matrixB[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   result = MultiplyMatrix(matrixA, matrixB)
   fmt.Println("The results of multiplication of matrix A & B: ")
   for i := 0; i < 3; i++ {
      for j := 0; j < 2; j++ {
         fmt.Print(result[i][j], "\t")
      }
      fmt.Println()
   }
}

输出

The first matrix is:
0  1  2
4  5  6
8  9 10
The second matrix is:
10  11
13  14
16  17
The results of multiplication of matrix A & B:
45   48
201  216
357  384

结论

我们成功地使用多维数组编译和执行了一个用于矩阵相乘的golang程序,附带了示例。在第一个示例中,我们在main()函数中使用了for循环来实现逻辑,而在第二个示例中,我们使用了一个外部用户定义的函数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程