[SciPy-User] A newbie question on matrix multiplication

Angus McMorland amcmorl@gmail....
Thu Mar 25 08:41:57 CDT 2010


On 25 March 2010 09:29, Graziano Mirata <graziano.mirata@lu.unisi.ch> wrote:
> Hi to everybody,
> i am new here and above all I am new in python. After years of matlab
> programming I finally have switched to Python.
>
> Coming to question: i am trying to multiply a matrix transpose by itself but
> i've got an error on the shape. But it is impossible. Here there is the
> code:
>
> type(f)
> Out[213]: <type 'numpy.ndarray'>
>
> In [214]: type(ft)
> Out[214]: <type 'numpy.ndarray'>
>
> In [215]: f.size
> Out[215]: 12
>
> In [216]: f.shape
> Out[216]: (4, 3)
>
> In [217]: ft = f.transpose()
>
> In [218]: ft.shape
> Out[218]: (3, 4)
>
> In [219]: ft*f

In python the * operator isn't overloaded to mean matrix
multiplication, meaning that ft * f tries to do element-wise
multiplication, and (3,4) * (4,3) aren't compatible shapes for that.
Try np.dot(ft, f) instead.

Angus.
-- 
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh


More information about the SciPy-User mailing list