简体   繁体   中英

numpy matrix multiplication

I am trying to figure out how to do a 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

to 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()

It seems like there must be a more efficient way of doing this though

利用广播

(a.T * b).T

The alternative to transposing a is to change the shape of b to make broadcasting give the result you're looking for:

a * b[:, np.newaxis]

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

array([[11],
       [12]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM