# [Numpy-discussion] Multi-dimensional indexing

Daran L. Rife drife@ucar....
Tue Sep 22 00:00:46 CDT 2009

```I forgot to mention that the second array, which I wish
to conditionally select elements from using tmax_idx,
has the same dimensions as the "speed" array, That is,

(ntimes, nlon, nlat) = U.shape

And tmax_idx has dimensions of (nlon, nlat).

Daran

--

> My apology for the simplemindedness of my question. I've
> been a long time user of NumPy and its predecessor Numeric,
> but am struggling to understand "fancy indexing" for multi-
> dimensional arrays. Here is the problem I am trying to solve.
>
> Suppose I have an 3-D array, named "speed" whose first dimen-
> sion is time, and the second and third dimensions are latitude
> and longitude. Further suppose that I wish to find the time
> where the values at each point are at their maximum. This can
> easily be done with the following code:
>
>>>> tmax_idx = np.argsort(speed, axis=0)
>
> I now wish to use this tmax_idx array to conditionally select
> the values from a separate array. How can this be done with
> fancy indexing? I've certainly done this sort of selection
> with index arrays in 1D, but I can not wrap my head round the
> multi-dimensionl index selection, even after carefully studying
> the excellent indexing documentation and examples on-line. I'd
> like to learn how to do this, to avoid the brute force looping
> solution of:
>
> mean_u = np.zeros((nlon, nlat), dtype=np.float32)
>
> for i in xrange(nlon):
>     for j in xrange(nlat):
>         mean_u[i,j] = U[max_spd_idx[i,j],i,j]
>
> As you know, this is reasonably fast for modest-sized arrays,
> but is far more expensive for large arrays.
>
>
>
>
> Sincerely,
>
>
> Daran Rife
>
>
>
>

```