Numpy transpose multiplication problem

38,922

Solution 1

You might find this tutorial useful since you know MATLAB.

Also, try multiplying testmatrix with the dot() function, i.e. numpy.dot(testmatrix,testmatrix.T)

Apparently numpy.dot is used between arrays for matrix multiplication! The * operator is for element-wise multiplication (.* in MATLAB).

Solution 2

You're using element-wise multiplication - the * operator on two Numpy matrices is equivalent to the .* operator in Matlab. Use

prod = numpy.dot(testmatrix, testmatrix.T)
Share:
38,922

Related videos on Youtube

Virgiliu
Author by

Virgiliu

01101000 01100001 01101000 01100001 00100000 01101101 01100001 01100100 01100101 00100000 01111001 01101111 01110101 00100000 01101100 01101111 01101111 01101011 00100001

Updated on July 09, 2022

Comments

  • Virgiliu
    Virgiliu almost 2 years

    I tried to find the eigenvalues of a matrix multiplied by its transpose but I couldn't do it using numpy.

    testmatrix = numpy.array([[1,2],[3,4],[5,6],[7,8]])
    prod = testmatrix * testmatrix.T
    print eig(prod)
    

    I expected to get the following result for the product:

    5    11    17    23
    11    25    39    53
    17    39    61    83
    23    53    83   113
    

    and eigenvalues:

    0.0000
    0.0000
    0.3929
    203.6071
    

    Instead I got ValueError: shape mismatch: objects cannot be broadcast to a single shape when multiplying testmatrix with its transpose.

    This works (the multiplication, not the code) in MatLab but I need to use it in a python application.

    Can someone tell me what I'm doing wrong?

  • BallpointBen
    BallpointBen over 7 years
    PEP 465 allows the use of the infix @ operator: mat1 @ mat2