使用NumPy库来实现两个3*3矩阵的运算。具体代码如下:

import numpy as np

# 定义两个3*3矩阵
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

# 矩阵加法
addition = matrix1 + matrix2
print("矩阵加法结果:")
print(addition)

# 矩阵减法
subtraction = matrix1 - matrix2
print("矩阵减法结果:")
print(subtraction)

# 矩阵乘法
multiplication = np.dot(matrix1, matrix2)
print("矩阵乘法结果:")
print(multiplication)

运行结果如下:

矩阵加法结果:
[[10 10 10]
 [10 10 10]
 [10 10 10]]
矩阵减法结果:
[[-8 -6 -4]
 [-2  0  2]
 [ 4  6  8]]
矩阵乘法结果:
[[ 30  24  18]
 [ 84  69  54]
 [138 114  90]]

其中,矩阵加法使用+运算符,矩阵减法使用-运算符,矩阵乘法使用np.dot()函数。

Python 3x3矩阵运算:加法、减法、乘法示例

原文地址: https://www.cveoy.top/t/topic/pwKE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录