我试图创建一个循环来执行以下操作:
两个矩阵,A和B,我想分别从B的每个元素中减去A中的第一个元素,然后转到A的下一个元素,然后将结果保存到一个新的矩阵C中。
A和B= 2x2矩阵的示例:
a1 = np.subtract(b[0, 0], a[0, 0]) #difference between all elements in b and first element in a
a2 = np.subtract(b[0, 1], a[0, 0])
a3 = np.subtract(b[1, 0], a[0, 0])
a4 = np.subtract(b[1, 0], a[0, 0])
b1 = np.subtract(b[0, 0], a[0, 1]) #difference between all elements in b and second element in a
b2 = np.subtract(b[0, 1], a[0, 1])
b3 = np.subtract(b[1, 0], a[0, 1])
b4 = np.subtract(b[1, 1], a[0, 1])
c1 = np.subtract(b[0, 0], a[1, 0]) #difference between all elements in b and third element in a
c2 = np.subtract(b[0, 1], a[1, 0])
c3 = np.subtract(b[1, 0], a[1, 0])
c4 = np.subtract(b[1, 1], a[1, 0])
d1 = np.subtract(b[0, 0], a[1, 1]) #difference between all elements in b and fourth element in a
d2 = np.subtract(b[0, 1], a[1, 1])
d3 = np.subtract(b[1, 0], a[1, 1])
d4 = np.subtract(b[1, 1], a[1, 1])
q = np.array([[a1, a2, a3, a4], [b1, b2, b3, b4], [c1, c2, c3, c4], [d1, d2, d3, d4]])
q发布于 2022-02-14 13:07:29
这是一个for循环,用于执行以下操作:
for ele in np.nditer(a):
a_sub = np.tile(np.array([ele]),(b.shape[0], b.shape[1]))
b -= a_subhttps://stackoverflow.com/questions/71016544
复制相似问题