Numpy:将一维索引转换为多维索引
尽管数组是多维的,但许多数组方法都会返回单个索引。例如:
a = rand(2,3)
z = a.argmax()
对于二维,很容易找到最大元素的矩阵索引:
a[z/3, z%3]
但是对于更多维度,它可能会变得烦人。 Numpy/Scipy 是否有一种简单的方法在给定一个(折叠)维度的索引的情况下返回多个维度的索引?谢谢。
Many array methods return a single index despite the fact that the array is multidimensional. For example:
a = rand(2,3)
z = a.argmax()
For two dimensions, it is easy to find the matrix indices of the maximum element:
a[z/3, z%3]
But for more dimensions, it can become annoying. Does Numpy/Scipy have a simple way of returning the indices in multiple dimensions given an index in one (collapsed) dimension? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
知道了!
Got it!
我不知道有什么内置函数可以满足您的要求,但是这个函数在哪里
已经出现在我面前,我意识到我真正想做的是:
给定两个具有相同形状的数组 a,b,找到 b 中的元素
与 a 的最大元素相同的位置(相同的 [i,j,k...] 位置)
为此,快速的 numpy-ish 解决方案是:
Vince Marchetti
I don't know of an built-in function that does what you want, but where this
has come up for me, I realized that what I really wanted to do was this:
given 2 arrays a,b with the same shape, find the element of b which is in
the same position (same [i,j,k...] position) as the maximum element of a
For this, the quick numpy-ish solution is:
Vince Marchetti