[Numpy-discussion] array vector elementwise multiplication

Charles R Harris charlesr.harris@gmail....
Tue Feb 3 19:09:52 CST 2009


On Tue, Feb 3, 2009 at 10:19 AM, Gideon Simpson <simpson@math.toronto.edu>wrote:

> I have an M x N matrix A and two vectors, an M dimensional vector x
> and an N dimensional vector y.  I would like to be able to do two
> things.
>
> 1.  Multiply, elementwise, every column of A by x
>
> 2.  Multiply, elementwise, every row of A by y.
>
> What's the "quick" way to do this in numpy?
>

In [1]: M = ones((3,3))

In [2]: x = arange(3)

In [3]: M*x
Out[3]:
array([[ 0.,  1.,  2.],
       [ 0.,  1.,  2.],
       [ 0.,  1.,  2.]])

In [4]: x[:,newaxis]*M
Out[4]:
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 2.,  2.,  2.]])

 Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090203/940a466d/attachment-0001.html 


More information about the Numpy-discussion mailing list