[Numpy-discussion] Broadcasting question

Charles R Harris charlesr.harris@gmail....
Fri May 2 00:18:42 CDT 2008


On Thu, May 1, 2008 at 10:58 PM, Chris.Barker <Chris.Barker@noaa.gov> wrote:

> Hi all,
>
> I have a n X m X 3 array, and I a n X M array. I want to assign the
> values in the n X m to all three of the slices in the bigger array:
>
> A1 = np.zeros((5,4,3))
> A2 = np.ones((5,4))
> A1[:,:,0] = A2
> A1[:,:,1] = A2
> A1[:,:,2] = A2
>

A1 += A2[:,:,newaxis] is one way. Or you could just start by stacking copies
of A2 and swapping axes around:

In [5]: A1 = array([A2.T]*3).T

In [6]: A1.shape
Out[6]: (4, 5, 3)

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080501/ac977182/attachment.html 


More information about the Numpy-discussion mailing list