I want to get the maximum and minimum value from a numpy array over multiple ranges. I found this solution for getting only the maximum over multiple ranges when the ranges are uniform. However, my ranges are not always uniform.
So if there is an array that has the numbers 1-9 and the ranges are the indices [0-2], [3-6], [7-8], I would like to get the max and min value from each range.
I have posted a possible solution below, but I wonder if numpy has a method to do this without a loop.
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9])
slices = [np.arange(0,3), np.arange(3,7), np.arange(7,9)]
max = np.empty(0)
min = np.empty(0)
for slice in slices:
max = np.concatenate([max, np.max(a[slice])])
min = np.concatenate([min, np.min(a[slice])])
min_and_max = np.concatenate([min,max])
np.ndarrayobjects had theconcatenatemethod as an attribute (relevant). Also looks like a mismatch of dims?np.ndarray.numpymethod that could operate on the list of slices?" That way I could avoid using a loop and repeated concatenation and the inherent inefficiency of repeated concatenation.