Hi Bryan:

On Tue, Apr 16, 2013 at 1:21 PM, Bryan Woods wrote:
I'm trying to do something that at first glance I think should be simple but I can't quite figure out how to do it. The problem is as follows:

I have a 3D grid Values[Nx, Ny, Nz]

I want to slice Values at a 2D surface in the Z dimension specified by Z_index[Nx, Ny] and return a 2D  slice[Nx, Ny].

It is not as simple as Values[:,:,Z_index].

I tried this:
>>> values.shape
(4, 5, 6)
>>> coords.shape
(4, 5)
>>> slice = values[:,:,coords]
>>> slice.shape
(4, 5, 4, 5)
>>> slice = np.take(values, coords, axis=2)
>>> slice.shape
(4, 5, 4, 5)
>>>

Obviously I could create an empty 2D slice and then fill it by using np.ndenumerate to fill it point by point by selecting values[i, j, Z_index[i, j]]. This just seems too inefficient and not very pythonic.

The following should work:

>>> values.shape
(4,5,6)
>>> coords.shape
(4,5)
>>> values[np.arange(values.shape[0])[:,None],
...        np.arange(values.shape[1])[None,:],
...        coords].shape
(4, 5)

Essentially we extract the values we want by values[I,J,K] where the indices I, J and K are each of shape (4,5) [or broadcast-able to that shape].