numpy array product driving me mad

Mr. Twister mr at twister.com
Fri Mar 20 08:46:58 EDT 2015


Hi everyone.

Hope you can help me overcome this "noob" issue.

I have two numpy arrays:

>>> P
array([[[ 2,  3],
        [33, 44],
        [22, 11],
        [ 1,  2]]])
>>> R
array([0, 1, 2, 3])

the values of these may of course be different. The important fact is that:

>>> P.shape
(1, 4, 2)
>>> R.shape
(4,)

where the number 4 in the shape of both P and R may be another number as well
(same on both).


What I'd like to get is a new array Q with same shape as P so that the nth pair
of Q is the nth pair of P multiplied by the nth element of R. I.e., in the above
case it should produce:

>>> Q
array([[[ 0,  0],
        [33, 44],
        [44, 22],
        [ 3,  6]]])


Is there a direct, single expression command to get this result? I have tried
all I could imagine, including .T, extend_dims, h/vstack, repeat..., so far with
no success.

Please, any help will be welcomed.

Thanks.




More information about the Python-list mailing list