python - Composing slices into a multidimensional slice for numpy array slicing -
if have 2 slice objects defined along 1 dimension each, possible combine them multidimensional slice object can used slice numpy array?
mat = np.zeros((10,10), dtype=np.uint8) s1 = slice(0,5) s2 = slice(0,5) mat[s1,s2] # want achieve effect 1 slice object slice2d = slice(s1, s2) # not throw error mat[slice2d] # not work
as pointed out @unutbu, multi-dimensional slice tuple
or list
of slice
objects, then:
slice2d = (s1, s2) mat[slice2d]
will work. similarly, can extend 3-d, ..., n-d arrays:
slice3d = (s1, s2, s3) ... slicend = (s1, s3, s3, ..., sn)
Comments
Post a Comment