<div dir="ltr"><div style>Hi Bryan:</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 16, 2013 at 1:21 PM, Bryan Woods <span dir="ltr"><<a href="mailto:bwoods@aer.com" target="_blank">bwoods@aer.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">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:<br>

<br>
I have a 3D grid Values[Nx, Ny, Nz]<br>
<br>
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].<br>
<br>
It is not as simple as Values[:,:,Z_index].<br>
<br>
I tried this:<br>
>>> values.shape<br>
(4, 5, 6)<br>
>>> coords.shape<br>
(4, 5)<br>
>>> slice = values[:,:,coords]<br>
>>> slice.shape<br>
(4, 5, 4, 5)<br>
>>> slice = np.take(values, coords, axis=2)<br>
>>> slice.shape<br>
(4, 5, 4, 5)<br>
>>><br>
<br>
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.<br></blockquote>
<div><br></div><div style>The following should work:</div><div style><div><br></div><div>>>> values.shape</div><div>(4,5,6)</div><div>>>> coords.shape</div><div>(4,5)</div><div>>>> values[np.arange(values.shape[0])[:,None],</div>
<div>...        np.arange(values.shape[1])[None,:],</div><div>...        coords].shape</div><div>(4, 5)</div><div><br></div><div style>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].</div>
<div style><br></div></div></div></div></div>