## Python: Find largest array index along a specific dimension which is greater than a threshold

Question

Lets say I have a 4-D numpy array (ex: `np.rand((x,y,z,t))`

) of data with dimensions corresponding to X,Y,Z, and time.

For each X and Y point, and at each time step, I want to find the largest index in Z for which the data is larger than some threshold `n`

.

So my end result should be an X-by-Y-by-t array. Instances where there are no values in the Z-column greater than the threshold should be represented by a 0.

I can loop through element-by-element and construct a new array as I go, however I am operating on a very large array and it takes too long.

Show source

## Answers ( 2 )

Unfortunately, following the example of Python builtins, numpy doesn't make it easy to get the

lastindex, although thefirstis trivial. Still, something likegives me

(There's probably a slicker way to do the flipping but it's the end of day here and my brain is shutting down. :-)

Here's a

fasterapproach -Runtime test -