矩阵是一个由许多数字按行和列排列的二维数组。两个矩阵的相加是将两个矩阵的对应元素相加,并将和放置在结果矩阵的对应位置。只有当两个矩阵的行数和列数相等时,这才可能。
在 Python 中,多维数组是使用列表或 NumPy 数组创建的。列表数据结构可以接受列表作为元素,这样我们就可以轻松创建矩阵。此外,Numpy 模块提供了多种处理多维数组的方法。
输入输出场景
两个矩阵的加法
[a11, a12, a13] [b11, b12, b13] [a11+b11, a12+b12, a13+b13] [a21, a22, a23] + [b21, b22, b23] = [a21+b21, a22+b22, a23+b23] [a31, a32, a33] [b31, b32, b33] [a31+b31, a32+b32, a33+b33]
在本文中,我们将了解如何在 python 中使用多维数组将两个矩阵相加。
使用for循环
在这里,我们将使用嵌套的for循环来遍历给定输入矩阵的每一行和每一列。在每次迭代中,我们将添加两个输入矩阵的相应元素,并将它们存储在结果矩阵中。
示例
# Defining the matrix using multidimensional arrays matrix_a = [[1,2,3], [4 ,5,6], [7 ,8,9]] matrix_b = [[1,2,3], [4 ,5,6], [7 ,8,9]] #function for displaying matrix def display(matrix): for row in matrix: print(row) print() # display two input matrices print('The first matrix is defined as:') display(matrix_a) print('The second matrix is defined as:') display(matrix_b) # Initializing Matrix with all 0s result = [[0, 0, 0],[0, 0, 0],[0, 0, 0]] # Add two matrices for i in range(len(matrix_a)): # iterate through rows for j in range(len(matrix_a[0])): # iterate through columns result[i][j] = matrix_a[i][j] + matrix_b[i][j] print('The addition of two matrices is:') display(result)
输出
The first matrix is defined as: [1, 2, 3] [4, 5, 6] [7, 8, 9] The second matrix is defined as: [1, 2, 3] [4, 5, 6] [7, 8, 9] The addition of two matrices is: [2, 4, 6] [8, 10, 12] [14, 16, 18]
将两个输入矩阵对应元素的和存储在我们最初用全零创建的结果矩阵中。
使用列表理解
列表理解提供了最短的语法来构建列表,而无需在 for 循环之前初始化空列表来逐一附加值。
示例
此示例的工作方式与前面的示例类似,但不同之处在于我们使用列表理解而不是创建全零的结果矩阵。
# Defining the matrix using multidimensional arrays matrix_a = [[1,2,5], [1,0,6], [9,8,0]] matrix_b = [[0,3,5], [4,6,9], [1,8,0]] #function for displaying matrix def display(matrix): for row in matrix: print(row) print() # display two input matrices print('The first matrix is defined as:') display(matrix_a) print('The second matrix is defined as:') display(matrix_b) # Add two matrices result = [[matrix_a[i][j] + matrix_b[i][j] for j in range(len(matrix_a[0]))] for i in range(len(matrix_a))] print('The addition of two matrices is:') display(result)
输出
The first matrix is defined as: [1, 2, 5] [1, 0, 6] [9, 8, 0] The second matrix is defined as: [0, 3, 5] [4, 6, 9] [1, 8, 0] The addition of two matrices is: [1, 5, 10] [5, 6, 15] [10, 16, 0]
使用NumPy数组
Python 中的 NumPy 模块有许多内置函数可以处理多维数组。通过使用这些数组,我们可以轻松地将两个矩阵相加。
示例
在此示例中,我们将使用 numpy.array() 方法创建两个多维数组。然后在两个数组之间应用加法运算符。
import numpy as np # Defining the matrix using numpy array matrix_a = np.array([[1,2,5], [1,0,6], [9,8,0]]) matrix_b = np.array([[0,3,5], [4,6,9], [1,8,0]]) # display two input matrices print('The first matrix is defined as:') print(matrix_a) print('The second matrix is defined as:') print(matrix_b) # Add two matrices result = matrix_a + matrix_b print('The addition of two matrices is:') print(result)
输出
The first matrix is defined as: [[1 2 5] [1 0 6] [9 8 0]] The second matrix is defined as: [[0 3 5] [4 6 9] [1 8 0]] The addition of two matrices is: [[ 1 5 10] [ 5 6 15] [10 16 0]]
我们只需在numpy数组matrix_a和matrix_b之间应用加法运算符(+)来添加多维数组。
以上就是使用多维数组编写的Python程序,用于将两个矩阵相加的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。