问题描述
我有一个numpy的阵列形状 [1000,1000,1000,3]
,是最后的尺寸,大小3,是包含三维空间向量部件的三胞胎。如何使用 nditer
来遍历每个三重?像这样的:
在np.nditer VEC(my_array,op_flags = ['只写'< ???>]):
VEC = np.array(东西)
我以前就解决了这个问题,但这里有一个简单的例子:
VEC = np.arange(2 * 2 * 2 * 3).reshape(2,2,2,3)
它= np.ndindex(2,2,2)
因为我在里面:
打印(VEC [I])
生产
[0 1 2]
[3 4 5]
[6 7 8]
[9 10 11]
[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]
ndindex
构建围绕大小的虚拟阵列多指数
迭代器,你给它(这里(2,2,2)
),并返回一个接下来
方法一起。
所以,你可以使用 ndindex
原样,或者用它作为一种模式为在 nditer
。
I have a Numpy array with shape [1000, 1000, 1000, 3]
, being the last dimension, sized 3, is contains the triplets of 3D spatial vectors components. How can I use nditer
to iterate over each triplet? Like this:
for vec in np.nditer(my_array, op_flags=['writeonly', <???>]):
vec = np.array(something)
I've addressed this question before, but here's a short example:
vec=np.arange(2*2*2*3).reshape(2,2,2,3)
it=np.ndindex(2,2,2)
for i in it:
print(vec[i])
producing:
[0 1 2]
[3 4 5]
[6 7 8]
[ 9 10 11]
[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]
ndindex
constructs a multi-index
iterator around a dummy array of the size you give it (here (2,2,2)
), and returns it along with a next
method.
So you can use ndindex
as is, or use it as a model for constructing your on nditer
.
这篇关于如何通过numpy的数组的初始迭代维度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!