There are several posts for slicing 2D and 3D NumPy arrays, but in my program dimension of the array is not known. Consider a NumPy ndarray A
of arbitrary dimension n
(positive integer), and shape D=[d1,...,dn]
(di
s nonnegative integers), For example:
import numpy as np
D=np.array([2,3,4,5])
A=np.random.rand(*D)
Now I need to extract a block of A
starting from D1=[d11,...,d1n]
to D2=[d21,...,d2n]
, where for all 0<i<=n : 0<=d1i<=d2i<=di
. Something like:
A[D1:D2]
If I new n
then I could simple use A[d11:d21,...,d1i:d2i,...,d1n:d2n]
, but that's not the case for me. I would appreciate if you could help me know what is the most efficient way of cropping A
given D1
and D2
.