I have a function that accepts an multi-dimensional array, an axis number and the index I would like to get:
def get_slice(my_array, dimension, index):
if dimension == 0:
slice = my_array[index, :, :]
elif dimension == 1:
slice = my_array[:, index, :]
else:
slice = my_array[:, :, index]
return np.squeeze(slice)
But, I now have to change the code to accept 4 dimensions and I was wondering if there is a more general way to do this in python?
So, I am looking for a function that accepts an general n-dimension array, a dimension (axis) and an index to select on that dimension/axis and returns me the entire slice of that index on that dimension.