<div dir="ltr"><div>A slightly different way to look at it (I don't think it is exactly the same problem, but the description reminded me of it):<br><br><a href="http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html">http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html</a><br><br></div><div>(and I think there are some things that can be done to make that faster, but I don't recall it right now)<br><br></div>Ben Root<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 16, 2013 at 4:35 PM, Bradley M. Froehle <span dir="ltr"><<a href="mailto:brad.froehle@gmail.com" target="_blank">brad.froehle@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi Bryan:</div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">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></div><div>The following should work:</div><div><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>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><br></div></div></div></div></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>