Looking for the solution not using 'for loops' in an ndarray processing

Viewed 39

I'd like to find the solution not using the for loops in the following: obtaining a mean of five 2D ndarrays using the min-max rejection.
Thanks.

stack = np.empty((0, 100, 100))
stack = np.append(stack, data1[np.newaxis, :], axis=0)
stack = np.append(stack, data2[np.newaxis, :], axis=0)
stack = np.append(stack, data3[np.newaxis, :], axis=0)
stack = np.append(stack, data4[np.newaxis, :], axis=0)
stack = np.append(stack, data5[np.newaxis, :], axis=0)

amax = np.argmax(stack, axis=0)
amin = np.argmin(stack, axis=0)

mask = np.zeros((5, 100, 100), dtype=bool)

for j in range(100):
    for i in range(100):
        nmax = amax[j, i]
        nmin = amin[j, i]
        mask[nmax, j, i] = True
        mask[nmin, j, i] = True

stack_tmp = np.ma.masked_array(stack, mask=mask)
stack_minmax = np.ma.mean(stack_tmp, axis=0).data
0 Answers
Related