Matrix multiplication

I am trying to figure out how to do some kind of scalar matrix multiplication in numpy.

I have

a = array(((1,2,3),(4,5,6))) b = array((11,12)) 

and i want to do

 a op b 

will result in

 array(((1*11,2*11,3*11),(4*12,5*12,6*12)) 

right now i am using the following expression

c = a * array ((b, b, b)). transpose ()

There seems to be a more efficient way to do this, though

+4
source share
2 answers

Using broadcasting :

 (aT * b).T 
+7
source

An alternative to transposing a is changing the shape of b so that the translation produces the result you are looking for:

 a * b[:, np.newaxis] 

Note that adding a new axis to b gives the following array:

 array([[11], [12]]) 
+1
source

All Articles