[Numpy-discussion] array multiplication

Robert Kern robert.kern at gmail.com
Wed Sep 16 17:40:00 EDT 2009


2009/9/16 Ernest Adrogué <eadrogue at gmx.net>:
> Hi,
>
> I have two 1-d arrays (a and b), and I want to create a
> third 2-d array, whose rows are of the form a[i]*b:
>
> c = np.zeros((len(a),b))
>
> c[0] = a[0]*b
> c[1] = a[1]*b
> .
> .
> .
>
> Is there an easy way to do this (e.g, without a loop)?

c = a[:,np.newaxis] * b

http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list