使用多维数组的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之间应用加法运算符(+)来添加多维数组。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP