python - NumPy:矩阵点积与 MATLAB 不兼容

标签 python matlab numpy

我正在尝试在 NumPy 中进行一些简单的计算。

但是突然给出了与MATLAB计算不同的结果。

这是 MATLAB 示例:

load temp; % here are the source matrices located

resp = ang_stv' * tmp;
respC = resp.'; % this is our variable to compare with Python

cd(fileparts(mfilename('fullpath')));
save('arythm_test.mat');

在这里我尝试用 Python 计算相同的结果:

dump_data = sp.io.loadmat("arythm_test.mat")

tmp = dump_data["tmp"]
ang_stv = dump_data["ang_stv"]
ref_resp = dump_data["respC"]

our_resp = np.dot(ang_stv.swapaxes(0, 1), tmp).swapaxes(0, 1)

np.testing.assert_allclose(our_resp, ref_resp)

你能告诉我,我做错了什么吗?又该如何应对呢?

附注这里是temp.matarythm_test.mat

最佳答案

答案很简单:MATLAB 中的 .' 运算符表示简单转置,而 ' 运算符表示复杂共轭转置。哎哟...

tmp = dump_data["tmp"]
ang_stv = dump_data["ang_stv"]

print "tmp.shape", tmp.shape
print "ang_stv.shape", ang_stv.shape

ref_resp = dump_data["respC"]

our_resp = np.dot(np.conj(ang_stv.swapaxes(0, 1)), tmp).swapaxes(0, 1)

关于python - NumPy:矩阵点积与 MATLAB 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368894/

相关文章:

matlab - Matlab ODE 求解器的多个输出

matlab - 如何重组 histcounts 以与二维矩阵一起使用

python - 使用 numpy 数组计算函数返回 inf 和 nan

python-2.7 - 在python中使用Abs查找函数的导数

python - pickling dict 继承类缺少内部值

python - 使用 Tkinter 插入文本框

python - 为什么 utorrents Magnet 到 Torrent 文件的获取速度比我的 python 脚本更快?

Python:在子程序中使用模板和替换

parallel-processing - 使用网络计算机的matlab并行处理

python - tensorflow py_func 很方便,但使我的训练步骤非常慢。