Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'element': 0.07; 'cc:addr :python-list': 0.11; '(1,': 0.16; '(same': 0.16; '0],': 0.16; '3],': 0.16; 'direct,': 0.16; 'i.e.,': 0.16; 'multiplied': 0.16; 'numpy': 0.16; 'subject:array': 0.16; 'url:html)': 0.16; 'wrote:': 0.18; 'shape': 0.19; 'command': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'array': 0.29; "i'm": 0.30; 'another': 0.32; "i'd": 0.34; 'received:84': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'fact': 0.38; 'sure': 0.39; 'read': 0.60; 'expression': 0.60; 'new': 0.61; 'course': 0.61; 'content-disposition:inline': 0.62; 'myself': 0.63; 'url:user': 0.65; 'different.': 0.84; 'received:82.223': 0.84; 'broadcasting': 0.91; 'mr.': 0.98 Date: Fri, 20 Mar 2015 14:11:45 +0100 From: Manolo =?iso-8859-1?Q?Mart=EDnez?= To: "Mr. Twister" Cc: python-list@python.org Subject: Re: numpy array product driving me mad References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426857727 news.xs4all.nl 2863 [2001:888:2000:d::a6]:42566 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87776 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