Python 使用多维数组添加两个矩阵
矩阵是一个由许多数字按行列排列的二维数组。两个矩阵的相加是将两个矩阵的相应元素相加,并将和放置在结果矩阵的相应位置中。只有当两个矩阵的行数和列数都相等时,才能进行相加。
在 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之间的加法运算符(+)来添加多维数组。