假设矩阵B已经存在,我们可以使用numpy库来构建矩阵A:

import numpy as np

# 构建矩阵B
B = np.array([[1, 0, 3], [0, 2, 0], [0, 0, 0]])

# 构建矩阵A
A = np.where(B == 0, 1, 0)

print("矩阵A:")
print(A)

输出结果:

矩阵A:
[[0 1 0]
 [1 0 1]
 [1 1 1]]

解释:我们使用np.where函数来构建矩阵A,其中B == 0表示矩阵B中为0的位置,1表示将这些位置设为1,0表示其他位置设为0。

python代码构建一个矩阵A大小与矩阵B相等矩阵B中为0的位置给A定义为1

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

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