I have two numpy arrays of the same shape: dat_ara
and ref_ara
.
I would like perform an operation op_func
on axis = -1
of dat_ara
, however I would only like to operate on a selected slice of values in each array, the slice is specified when a threshold value thres
is crossed by the reference array ref_ara
.
For illustration, in the simple case where the arrays are just 2-dim, I have:
thres = 4
op_func = np.average
ref_ara = array([[1, 2, 1, 4, 3, 5, 1, 5, 2, 5],
[1, 2, 2, 1, 1, 1, 2, 7, 5, 8],
[2, 3, 2, 5, 1, 6, 5, 2, 7, 3]])
dat_ara = array([[1, 0, 0, 1, 1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1]])
We see that thres
is breached in the 5th, 7th and 3rd index of the 1st, 2nd and 3rd array in axis=0
of ref_ara
. Therefore the outcome I desire would be
out_ara = array([op_func(array([1, 0, 0, 1, 1, 1]),
op_func(array([1, 1, 1, 1, 1, 1, 1, 0]),
op_func(array([1, 0, 1, 1])])
This problem is difficult because it requires referencing to ref_ara
. If that were not the case, I could simple use numpy.apply_along_axis
.
I have tried expanding the dimensions of the two arrays to associate them to for computation, i.e.:
assos_ara = np.append(np.expand_dims(dat_ara, axis=-1), np.expand_dims(ref_ara, axis=-1), axis=-1)
But again, numpy.apply_along_axis
requires the input function to only operate on 1-dim arrays, and thus I still cannot utilise the function.
The only other way I know is to iterate through the arrays index wise, however, with the arrays having constant changing dimensions of the two arrays, this is a tricky matter, moreover, it is not computationally efficient.
I would like as much to use vectorised functions to aid this process. What is the most efficient way to go about it?