是否有一个numpy方法通过添加两个1D数组来创建2D数组? [英] Is there a numpy method to create a 2D array from the addition of two 1D arrays?
本文介绍了是否有一个numpy方法通过添加两个1D数组来创建2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上是s_{i,j} = a_i + b_j
如果n=len(a)
和m=len(b)
,则s.shape
为(n,m)
.因此,上面的加法运算不是可交换的.
if n=len(a)
and m=len(b)
, then s.shape
is (n,m)
. Thus, the addition above is not commutative.
它涉及一些轨迹:
s = np.tile(a.reshape(n,1),m).reshape(n,m) + np.tile(b,n).reshape(n,m)
但是我想知道numpy中是否已经有一些方法可以做到这一点.我找不到它.
but I was wondering if there's already some method in numpy for this. I could not find it.
推荐答案
是的,实际上是. numpy
ufuncs具有outer
方法:
Yes, there actually is. numpy
ufuncs have an outer
method:
s = np.add.outer(a, b)
这篇关于是否有一个numpy方法通过添加两个1D数组来创建2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文