【发布时间】:2014-11-11 23:15:01
【问题描述】:
我想使用numpy.ix_ 为二维值空间生成多维索引。但是,我需要使用子索引来查找一维的索引。例如,
assert subindex.shape == (ny, nx)
data = np.random.random(size=(ny,nx))
# Generator returning the index tuples
def get_idx(ny,nx,subindex):
for y in range(ny):
for x in range(nx):
yi = y # This is easy
xi = subindex[y,x] # Get the second index value from the subindex
yield (yi,xi)
# Generator returning the data values
def get_data_vals(ny,nx,data,subindex):
for y in range(ny):
for x in range(nx):
yi = y # This is easy
xi = subindex[y,x] # Get the second index value from the subindex
yield data[y,subindex[y,x]]
所以我想使用多维索引来索引data,而不是上面的for循环,使用numpy.ix_,我想我会有类似的东西:
idx = numpy.ix_([np.arange(ny), ?])
data[idx]
但我不知道第二维参数应该是什么。我猜应该是涉及到numpy.choose?
【问题讨论】:
-
不清楚你到底想要什么,你能举个例子吗?
-
Ashwini,我已经稍微更新了这个问题。是不是更清楚了?