<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Dec 3, 2014 at 4: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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style: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>
</blockquote><div><br></div><div>I don't think this would quite work... Even though it now has 3 dimensions (Nx, Ny, 1), B is still a single array, so when fancy indexing A with it, it will only be applied to the first axis, so the return will be of shape B.shape + A.shape[1:], that is (Nx, Ny, 1, Ny, Nz).</div><div><br></div><div>What you need to have is three indexing arrays, one per dimension of A, that together broadcast to the desired shape of the output C. For this particular case, you could do:</div><div><br></div><div>nx = np.arange(A.shape[0])[:, np.newaxis]</div><div>ny = np.arange(A.shape[1])</div><div>C = A[nx, ny, B]</div><div><br></div><div>To show that this works:</div><div><br></div><div><div>>>> A = np.arange(2*3*4).reshape(2, 3, 4)</div><div>>>> A</div><div>array([[[ 0,  1,  2,  3],</div><div>        [ 4,  5,  6,  7],</div><div>        [ 8,  9, 10, 11]],</div><div><br></div><div>       [[12, 13, 14, 15],</div><div>        [16, 17, 18, 19],</div><div>        [20, 21, 22, 23]]])</div></div><div><div>>>> B = np.random.randint(4, size=(2, 3))</div><div>>>> B</div><div>array([[2, 1, 2],</div><div>       [2, 0, 3]])</div></div><div><div>>>> nx = np.arange(A.shape[0])[:, None]</div><div>>>> ny = np.arange(A.shape[1])</div><div>>>> A[nx, ny, B]</div><div>array([[ 2,  5, 10],</div><div>       [14, 16, 23]])</div></div><div><br></div><div>Jaime</div><div><br></div></div>-- <br><div class="gmail_signature">(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.</div>
</div></div>