简体   繁体   中英

Substract Array of arrays from another array in numpy

Is there a numpy manner to compute next problem? I really want to use numpy because the array is much much larger than this example.

a = np.array([[5, 2, 3, 4], [6, 3, 6, 6], [9, 1, 4, 6]])
b = np.min(a,1)

print(a)
# [[5 2 3 4]
#  [6 3 6 6]
#  [9 1 4 6]]

print(b)
# [2 3 1]

print(a-b) # ValueError: operands could not be broadcast together with shapes (3,4) (3,) 

# What I want:
# [[3 0 1 2]
#  [3 0 3 3]
#  [8 0 3 5]]

你可以这样做:

print(a-b.reshape(-1,1))

This will do the job:

print(a-b.reshape(len(b),-1))

This prints

array([[3, 0, 1, 2],
       [3, 0, 3, 3],
       [8, 0, 3, 5]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM