<div dir="ltr">Posting in the correct thread now...<br><br><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><a href="http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html" target="_blank">http://mail.scipy.org/pipermail/numpy-discussion/2013-April/066269.html</a><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 Wed, Dec 3, 2014 at 7:02 PM, Stefan van der Walt <span dir="ltr"><<a href="mailto:stefan@sun.ac.za" target="_blank">stefan@sun.ac.za</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Catherine<br>
<span class=""><br>
On 2014-12-04 01:12:30, Moroney, Catherine M (398E) <<a href="mailto:Catherine.M.Moroney@jpl.nasa.gov">Catherine.M.Moroney@jpl.nasa.gov</a>> wrote:<br>
> I have an array "A" of shape (NX, NY, NZ), and then I have a second array "B" of shape (NX, NY)<br>
> that ranges from 0 to NZ in value.<br>
><br>
> I want to create a third array "C" of shape (NX, NY) that holds the<br>
> "B"-th slice for each (NX, NY)<br>
<br>
</span>Those two arrays can broadcast if you expand the dimensions of B:<br>
<br>
A: (NX, NY, NZ)<br>
B: (NX, NY, 1)<br>
<br>
Your result would be<br>
<br>
B = B[..., np.newaxis]  # now shape (NX, NY, 1)<br>
C = A[B]<br>
<br>
For more information on this type of broadcasting manipulation, see<br>
<br>
<a href="http://nbviewer.ipython.org/github/stefanv/teaching/blob/master/2014_assp_split_numpy/numpy_advanced.ipynb" target="_blank">http://nbviewer.ipython.org/github/stefanv/teaching/blob/master/2014_assp_split_numpy/numpy_advanced.ipynb</a><br>
<br>
and<br>
<br>
<a href="http://wiki.scipy.org/EricsBroadcastingDoc" target="_blank">http://wiki.scipy.org/EricsBroadcastingDoc</a><br>
<br>
Stéfan<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<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>
</div></div></blockquote></div><br></div>