Re: how to multiply two matrices with different size?

2016-11-18 Thread Ian Kelly
On Fri, Nov 18, 2016 at 6:51 PM,   wrote:
> Hi :)
> I'm trying to multiply two matrices that has different size.
>
> -code-
>
> import numpy as np
>
> a = np.random.randn(4, 3)
> b = np.random.randn(4, 1)
>
> print a
> print b
>
> -code-
>
> How should I multiply a and b so that the multipled matrix's size is 4*3?
>
> I want to multiply matrix 'b' to each row of matrix 'a'.
> So that if matrix a is
> [[1, 2, 3],
>   [2, 3, 4]]
> and b is
> [[2],
> [3]]
> a*b is
> [[2, 4, 6],
>   [4, 6 ,8]]
>
> Plz help me!
>
> if possible, plz use numpy

You don't need to do anything special.

>>> import numpy as np
>>> a = np.array([[1,2,3],[2,3,4]])
>>> b = np.array([[2], [3]])
>>> a
array([[1, 2, 3],
   [2, 3, 4]])
>>> b
array([[2],
   [3]])
>>> a * b
array([[ 2,  4,  6],
   [ 6,  9, 12]])

(I'm assuming this is actually the output you want. The output you
posted is jut a * 2.)
-- 
https://mail.python.org/mailman/listinfo/python-list


how to multiply two matrices with different size?

2016-11-18 Thread limetree377
Hi :)
I'm trying to multiply two matrices that has different size.

-code-

import numpy as np

a = np.random.randn(4, 3)
b = np.random.randn(4, 1)

print a
print b

-code-

How should I multiply a and b so that the multipled matrix's size is 4*3?

I want to multiply matrix 'b' to each row of matrix 'a'.
So that if matrix a is
[[1, 2, 3],
  [2, 3, 4]]
and b is
[[2],
[3]]
a*b is
[[2, 4, 6],
  [4, 6 ,8]]

Plz help me!

if possible, plz use numpy
-- 
https://mail.python.org/mailman/listinfo/python-list