[Numpy-discussion] Broadcasting question
Anne Archibald
peridot.faceted@gmail....
Fri May 2 01:17:13 CDT 2008
2008/5/2 Chris.Barker <Chris.Barker@noaa.gov>:
> 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
>
> However,it seems I should be able to broadcast that, so I don't have to
> repeat myself, but I can't figure out how.
All you need is a newaxis on A2:
In [2]: A = np.zeros((3,4))
In [3]: B = np.ones(3)
In [4]: A[:,:] = B[:,np.newaxis]
In [5]: A
Out[5]:
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
Anne
More information about the Numpy-discussion
mailing list