I have an instance of numpy ndarray, but of a variable size.
import numpy as np
dimensions = (4, 4, 4)
myarray = np.zeros(shape = dimensions)
In this case, I get a "cubic" shape of the array and if I want to index a slice of myarray
I can use myarray[:][:][0]
because I know there are 3 dimensions (I use 3 pairs of []
).
In case of 4 dimensions, I would use myarray[:][:][:][0]
. But since the number of dimensions may change, I cannot hard-code it this way.
How can I index a slice of such an array depending on the number of dimensions? Seems like a simple problem, cannot think of any solution though.
myarray[:][:][0]
do you actually meanmyarray[:, :, 0]
? The former is just equal tomyarray[0]
, the latter is not. – Alex Riley