Subracting all elements of array A from all elements of B?

依然范特西╮ 提交于 2019-12-01 19:59:13

Extend b to a 2D array case with np.newaxis/None and then let broadcasting play its part for a fast vectorized solution, like so -

a - b[:,None]

Sample run -

In [19]: a
Out[19]: array([1, 2, 3])

In [20]: b
Out[20]: array([1, 2, 3])

In [21]: a - b[:,None]
Out[21]: 
array([[ 0,  1,  2],
       [-1,  0,  1],
       [-2, -1,  0]])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!