获取长方体的所有边(3D NumPy数组)

时间:2022-06-25 20:05:26

Having a 3-dimensional numpy array A I want to get all the edges (imagine this array as a cuboid).

有一个三维numpy数组A我想得到所有的边(想象这个数组作为一个长方体)。

Well, A[0, 0, :] would give me one edge, A[0, -1, :] second one and A[:, -1, -1] yet another one... so all I'd have to do is get all permutations of 0, -1 and : and use them as indices. Zero and minus one are easy, but how would I do this with colon?

好吧,A [0,0,:]会给我一个边,A [0,-1,:]第二个和A [:, - 1,-1]还有一个......所以我只有要做的是得到0,-1和的所有排列:并将它们用作索引。零和一个很容易,但我怎么用冒号呢?

I can solve it the long way, but it's ugly and I bet there is some neat numpy solution to this. Something like:

我可以解决它很长的路,但它很难看,我打赌这有一个整洁的numpy解决方案。就像是:

for indices in permutations([0, -1, ':']):
    edge = A[indices]
    ...

What I want to do in the end is numpy.any() on the set of all edges to see if all edge-values are zero.

我最后要做的是在所有边的集合上使用numpy.any()来查看所有边值是否为零。

1 个解决方案

#1


1  

: is same as slice(None, None, None)

:与slice相同(None,None,None)

A[0, -1, :] is same as

A [0,-1,:]与...相同

obj = (0, -1, slice(None, None, None))
A[obj]

#1


1  

: is same as slice(None, None, None)

:与slice相同(None,None,None)

A[0, -1, :] is same as

A [0,-1,:]与...相同

obj = (0, -1, slice(None, None, None))
A[obj]