[英]Composing slices into a multidimensional slice for numpy array slicing
If I have two slice objects defined along one dimension each, is it possible to combine them to get a multidimensional slice object that can be used to slice a numpy array?如果我有两个切片对象分别沿一维定义,是否可以将它们组合起来以获得可用于切片 numpy 数组的多维切片对象?
mat = np.zeros((10,10), dtype=np.uint8)
s1 = slice(0,5)
s2 = slice(0,5)
mat[s1,s2] # I want to achieve this effect with one slice object
slice2d = slice(s1, s2) # does not throw an error
mat[slice2d] # but this does not work
As pointed out by @unutbu, what would be a multi-dimensional slice is actually a tuple
or list
of slice
objects, then:正如@unutbu 所指出的,多维切片实际上是一个tuple
或slice
对象list
,然后:
slice2d = (s1, s2)
mat[slice2d]
will work.将工作。 Similarly, you can extend this to 3-D, ..., ND arrays:同样,您可以将其扩展到 3-D, ..., ND 数组:
slice3d = (s1, s2, s3)
...
sliceNd = (s1, s3, s3, ..., sN)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.