# [Numpy-discussion] how to multiply the rows of a matrix by a different number?

Jose Borreguero borreguero@gmail....
Wed Mar 4 13:17:26 CST 2009

```Sweet!

I found that *M*b.reshape(10000,1)* will also do the trick. Any guess which
method is faster?

On Tue, Mar 3, 2009 at 9:11 PM, <josef.pktd@gmail.com> wrote:

> On Tue, Mar 3, 2009 at 8:53 PM, Jose Borreguero <borreguero@gmail.com>
> wrote:
> > I guess there has to be an easy way for this. I have:
> > M.shape=(10000,3)
> > N.shape=(10000,)
> >
> > I want to do this:
> > for i in range(10000):
> > M[i]*=N[i]
> > without the explicit loop
> >
>
> >>> M = np.ones((10,3))
> >>> N = np.arange(10)
> >>> N.shape
> (10,)
> >>> (N[:,np.newaxis]).shape
> (10, 1)
> >>> M*N[:,np.newaxis]
> array([[ 0.,  0.,  0.],
>       [ 1.,  1.,  1.],
>       [ 2.,  2.,  2.],
>       [ 3.,  3.,  3.],
>       [ 4.,  4.,  4.],
>       [ 5.,  5.,  5.],
>       [ 6.,  6.,  6.],
>       [ 7.,  7.,  7.],
>       [ 8.,  8.,  8.],
>       [ 9.,  9.,  9.]])
>
> >>> M *= N[:,np.newaxis]
> >>> M
>
> Josef
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090304/88bd0765/attachment.html
```