how to multiply two matrices with different size?
Ian Kelly
ian.g.kelly at gmail.com
Fri Nov 18 21:23:51 EST 2016
On Fri, Nov 18, 2016 at 6:51 PM, <limetree377 at gmail.com> 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.)
More information about the Python-list
mailing list