具有(n-1)d数组的索引n维数组 [英] Index n dimensional array with (n-1) d array
本文介绍了具有(n-1)d数组的索引n维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如虚拟示例中那样,沿给定维访问具有(n-1)维数组的n维数组的最优雅方法是什么
What is the most elegant way to access an n dimensional array with an (n-1) dimensional array along a given dimension as in the dummy example
a = np.random.random_sample((3,4,4))
b = np.random.random_sample((3,4,4))
idx = np.argmax(a, axis=0)
我现在如何使用idx a
访问以获取a
中的最大值,就像我曾经使用过a.max(axis=0)
一样?或如何检索b
中idx
指定的值?
How can I access now with idx a
to get the maxima in a
as if I had used a.max(axis=0)
? or how to retrieve the values specified by idx
in b
?
我考虑过使用np.meshgrid
,但是我认为这太过分了.请注意,尺寸axis
可以是任何有用的轴(0,1,2),并且事先未知.有没有一种优雅的方法可以做到这一点?
I thought about using np.meshgrid
but I think it is an overkill. Note that the dimension axis
can be any usefull axis (0,1,2) and is not known in advance. Is there an elegant way to do this?
推荐答案
查看全文