[英]Slicing a multidimensional array without a for loop
I have a 3D array r (1000 x 10 x 2000)
constructed as follows: 我有一个3D数组
r (1000 x 10 x 2000)
构造如下:
q = np.random.normal(size=(10,2000))
r = np.random.normal(loc=q, size=(1000,10,2000))
This array, r
, can be viewed as a 1000 x 10
matrix repeated 2000 times. 该阵列
r
可以被视为重复2000次的1000 x 10
矩阵。
I would like to reduce this array according to the following rule: 我想根据以下规则减少此数组:
The columns to be selected ca be obtained via: np.argmax(r[0], axis=0)
. 要选择的列可以通过以下方式获得:
np.argmax(r[0], axis=0)
。
The result should be a 1000 x 2000
matrix. 结果应该是
1000 x 2000
矩阵。
I wonder if it is possible to get something like that without using a for
loop or list comprehensions. 我想知道是否有可能在不使用
for
循环或列表推导的情况下获得类似的东西。
Here is a for
loop which achieves the above task: 这是一个
for
循环,它实现了上述任务:
x = []
for i, idx in enumerate(np.argmax(r[0], axis=0)):
x.append(r[:,idx,i])
x = np.array(x).T
The solution I figured looks like this: 我想到的解决方案看起来像这样:
r[:, np.argmax(r[0],axis=0), np.arange(2000)]
More elegant solutions are, of course, welcome. 当然,更优雅的解决方案是受欢迎的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.