我在numpy中有两个数组。第一个是2D数组,可以看作是向量列表。第二个是1D数组,可以看作是对2D数组的索引列表。
我想使用1D数组的索引选择2D数组的元素。目前我一直在做以下操作:
我想使用1D数组的索引选择2D数组的元素。目前我一直在做以下操作:
z=rnd.rand(2,10) # a list of 2d vectors of length 10
z_idx=rnd.randint(2,size=z.shape[1]) #indices selecting a dimension of the 2d vector
result=np.array([z[z_idx[i],i] for i in xrange(len(z_idx))])
但这样做非常缓慢。
在numpy中有更好的方法吗?