import numpy as np
matx = np.arange(9).reshape(3, 3)
print(matx)
print(type(matx))
print(matx != np.eye(3)) # 判断每个元素在对应的位置上是否不相等
print((matx != np.eye(3)).any()) # any判断是否有一个是TRUE,只要有一个为真就为真
print((matx != np.eye(3)).all()) # 所有的都为真才为真
matx2 = np.array([
[1, 0, 0],
[1, 1, 0],
[1, 1, 1],
])
print("*"*30)
print(matx2 != np.eye(3))
print((matx2 != np.eye(3)).any()) # 再次证实只要有一个为真结果就为真
print("-"*30+"nall")
print((matx2 != np.eye(3)).all()) # 所有的都为真才为真
print('#'*30)
# 矩阵乘法
print(matx2@np.eye(3))
执行结果:
[[0 1 2]
[3 4 5]
[6 7 8]]
[[ True True True]
[ True True True]
[ True True True]]
True
True
******************************
[[False False False]
[ True False False]
[ True True False]]
True
------------------------------
all
False
##############################
[[1. 0. 0.]
[1. 1. 0.]
[1. 1. 1.]]