Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87776

Re: numpy array product driving me mad

Date 2015-03-20 14:11 +0100
From Manolo Martínez <manolo@austrohungaro.com>
Subject Re: numpy array product driving me mad
References <meh4s3$sd4$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.38.1426857727.10327.python-list@python.org> (permalink)

Show all headers | View raw


On 03/20/15 at 01:46pm, Mr. Twister wrote:
 
> 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 think that you want 

    P * R[;,None]

Read about broadcasting
(http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html) for an
explanation. I'm never sure I understand it myself :)

Manolo
 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

numpy array product driving me mad "Mr. Twister" <mr@twister.com> - 2015-03-20 13:46 +0100
  Re: numpy array product driving me mad Manolo Martínez <manolo@austrohungaro.com> - 2015-03-20 14:11 +0100
  Re: numpy array product driving me mad Manolo Martínez <manolo@austrohungaro.com> - 2015-03-20 14:14 +0100
    Re: numpy array product driving me mad "Mr. Twister" <mr@twister.com> - 2015-03-20 14:42 +0100

csiph-web