Approach #1
The trick to finding the third one is subtracting from 3
those max and min indices. The borderline case would be when max and min indices are the same, i.e. all three elements along the last axis are the same, for which the third element index would also be the same.
Thus, we would have one solution like this -
max_idx = y.argmax(2)
min_idx = y.argmin(2)
rem_idx = np.where(max_idx == min_idx, max_idx, 3 - max_idx - min_idx)
out = (y[all_idx(max_idx, 2)] -2*y[all_idx(min_idx, 2)])/y[all_idx(rem_idx, 2)]
Helper function for indexing into y
with the indices -
# https://stackoverflow.com/a/46103129/ @Divakar
def all_idx(idx, axis):
grid = np.ogrid[tuple(map(slice, idx.shape))]
grid.insert(axis, idx)
return tuple(grid)
Approach #2
We could get the summation along the axis and subtract min and max values to get the third element and simply plug that into the formula -
maxv = np.max(y, axis =2)
minv = np.min(y, axis =2)
x = (maxv - 2*minv)/(y.sum(2) - maxv - minv)