[Numpy-discussion] dot() function question

Chris Barker chris.barker at noaa.gov
Thu Jun 28 14:14:31 EDT 2012


On Wed, Jun 27, 2012 at 2:38 PM,  <x.piter at gmail.com> wrote:
> How how can I perform matrix multiplication of two vectors?
> (in matlab I do it like a*a')

np.outer is a bit cleaner, I suppose, but you can exactly the same
thing you do with matlab if a is a column (single column 2-d array):

In [40]: a = np.arange(4).reshape((-1,1))

In [41]: a
Out[41]:
array([[0],
       [1],
       [2],
       [3]])

In [42]: np.dot(a,a.T)
Out[42]:
array([[0, 0, 0, 0],
       [0, 1, 2, 3],
       [0, 2, 4, 6],
       [0, 3, 6, 9]])

or, of course, 2 arrays to begin with:

In [13]: a = np.arange(4).reshape((4,1))

In [14]: b = np.arange(4).reshape((1,4))

In [15]: np.dot(a,b)
Out[15]:
array([[0, 0, 0, 0],
       [0, 1, 2, 3],
       [0, 2, 4, 6],
       [0, 3, 6, 9]])


-Chris


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list