fishbacp
fishbacp

Reputation: 1263

Given a 4-dimensional array, how do I compute a percentile, excluding from consideration those whose corresponding 3rd and 4th indices are equal

I have a four dimensional array, M, which has size 10-by-8-by-4-by-4. I want to compute the 99th percentile, p, of its 1280 entries, excluding from consideration those values whose corresponding third and fourth indices are equal.

For example, I would include all matrices M[:,:,2,3] but I would exclude the matrices M[:,:,i,i] where i=0,1,2,3.

What would be the most efficient means for accomplishing this and how could I compute the indices of the form [a,b,i,j], where 0<=a<=9, 0<=b<=7, 0<=i,j<=3 and where i and j are different and where M[a,b,i,j]>p ?

Upvotes: 0

Views: 120

Answers (1)

Captain Trojan
Captain Trojan

Reputation: 2921

First step is definitely to flatten the first two dimensions (maybe I missed something, but their distinction is redundant).

M = np.reshape(M, (80, 4, 4))

Then, you create a mask:

mask = np.repeat(np.expand_dims(np.eye(4), axis=0), 80, axis=0)

Then you create the masked array:

import numpy.ma as ma
masked_M = ma.masked_array(M, mask)

And now you can do anything with the masked array, including computing a percentile:

print(np.percentile(masked_M, 0.9))

Upvotes: 1

Related Questions