Golang 使用多维数组将两个矩阵相加

Golang 使用多维数组将两个矩阵相加

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

使用循环相加两个矩阵

现在让我们看一个使用循环相加两个矩阵的Go语言程序。

上述程序的算法

步骤1 - 导入fmt包。

步骤2 - 现在我们需要开始main()函数。

步骤3 - 然后我们创建了两个名为matrixA和matrixB的矩阵,并在它们中存储值。

步骤4 - 使用fmt.Println()函数将数组打印在屏幕上。

步骤5 - 初始化一个新的int类型矩阵来保存结果。

步骤6 - 使用for循环遍历两个矩阵来相加它们。

步骤7 - 第一个for循环用于获取矩阵的行数,而第二个for循环用于获取矩阵的列数。

步骤8 - 一旦循环结束,新矩阵将得到两个矩阵的和。

步骤9 - 使用for循环和fmt.Println()函数打印新矩阵的元素。

示例

package main
import (
   "fmt"
)

// calling the main() function.
func main() {
   var i, j int
   var matrixC [3][3]int
   matrixA := [3][3]int{
      {0, 1},
      {4, 5},
      {8, 9},
   }
   matrixB := [3][3]int{
      {10, 11, 12},
      {13, 14, 15},
      {16, 17, 18},
   }
   fmt.Println("The first matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 2; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
   fmt.Println()
   }
   fmt.Println()

   // printing the second matrix on the screen
   fmt.Println("The second matrix is:")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixB[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   fmt.Println("The results of addition of matrix A & B: ")
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         matrixC[i][j] = matrixA[i][j] + matrixB[i][j]
      }
   }
   for i = 0; i < 3; i++ {
      for j = 0; j < 3; j++ {
         fmt.Print(matrixC[i][j], "\t")
      }
      fmt.Println()
   }
}

输出

The first matrix is:
0   1   
4   5   
8   9   
The second matrix is:
10  11  12  
13  14      15  
16  17  18  

The results of addition of matrix A & B: 
1012    12  
17  19  15  
24  26  18

使用外部函数添加两个矩阵

在这个示例中,我们将使用用户定义的函数来添加两个矩阵。

上述程序的算法

步骤1 - 导入fmt包。

步骤2 - 创建一个函数来添加两个矩阵。

步骤3 - 在这个函数中使用make()函数来创建一个矩阵的切片,并使用range函数来迭代矩阵以找到和。

步骤4 - 开始main函数。

步骤5 - 初始化两个矩阵并存储元素,将矩阵打印到屏幕上。

步骤6 - 通过将两个矩阵作为参数传递给AddMatrices()函数来调用该函数。

步骤7 - 存储获得的结果并将其打印到屏幕上。

语法

func make ([] type, size, capacity)

在Go语言中,make函数用于创建数组/映射,它接受要创建的变量的类型,大小和容量作为参数。

func append(slice, element_1, element_2…, element_N) []T

append函数用于将值添加到数组切片。它接受多个参数。第一个参数是我们希望添加值的数组,后面是要添加的值。函数然后返回包含所有值的最终数组切片。

示例

package main
import (
   "fmt"
)

// creating a function to add matrices
func AddMatrix(matrix1 [3][3]int, matrix2 [3][3]int) [][]int {
   result := make([][]int, len(matrix1))
   for i, a := range matrix1 {
      for j, _ := range a {
         result[i] = append(result[i], matrix1[i][j]+matrix2[i][j])
      }
   }
   return result
}
func main() {
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   matrixB := [3][3]int{
      {10, 11},
      {13, 14},
      {16, 17},
   }
   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()
   }
   fmt.Println()

   // printing the second matrix on the screen
   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()

   // calling the AddMatrix() function
   result := AddMatrix(matrixA, matrixB)
   fmt.Println("The results of addition of matrix A & B: ")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; 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 addition of matrix A & B: 
10  12  2   
17  19  6   
24  26  10

结论

我们已经成功地编译和执行了一个用于矩阵相加的Go语言程序,并附带了示例。在第一个示例中,我们在main()函数中实现了逻辑,而在第二个示例中,我们使用了外部函数来实现上述逻辑。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程