Search code examples
pythonmatlabcode-conversion

Matrix multiplication renders different results when converting from MATLAB to python


I have an MxN matrix A where A is complex and apparently has some properties which renders a diagonal matrix for A'*A. A is also very large (256x16384) so I cannot think of a way to upload it here for reproducing.

In the MATLAB code, I have:

imshow(abs(A'*A),[]);

this produces the following image: enter image description here

The converted code in python:

tmp=np.dot(A.T,A)
plt.imshow(np.absolute(tmp))
plt.show()

produces:

enter image description here

The diagonal seemed to rotate. Is there any reason for this rotation? Am I doing the conversion correctly?


Solution

  • I guess that you're confusing between the transpose and the conjugate transpose operator.

    Matlab to Python:

    transpose:

    • Matlab: A.'
    • Python: A.T

    conjugate tranpose:

    • Matlab: A'
    • Python: A.conj().T

    So change your matlab code or your python code according to your need.