矩阵乘法导致MATLAB和NUMPY(?)中的值不同 [英] Matrix multiplication resulting in different values in MATLAB and NUMPY(?)
本文介绍了矩阵乘法导致MATLAB和NUMPY(?)中的值不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是矩阵
>> x = [2 7 5 9 2; 8 3 1 6 10; 4 7 3 10 1; 6 7 10 1 8;2 8 2 5 9]
Matlab给了我
>> mtimes(x',x)
ans =
124 124 94 122 154
124 220 145 198 179
94 145 139 101 121
122 198 101 243 141
154 179 121 141 250
但是,python(numpy)中的相同操作(对相同数据)产生不同的结果.我不明白为什么?
However, the same operation(on same data) in python(numpy) produces different result. I'm unable to understand why?
import numpy as np
a = [[2, 7, 5, 9, 2],[8,3,1,6,10],[4,7,3,10,1],[6,7,10,1,8],[2,8,2,5,9]]
x = np.array(a)
print 'A : ',type(x),'\n',x,'\n\n'
# print np.transpose(A)
X = np.multiply(np.transpose(x),x)
print "A'*A",type(X),'\n',X
产生
A : <type 'numpy.ndarray'>
[[ 2 7 5 9 2]
[ 8 3 1 6 10]
[ 4 7 3 10 1]
[ 6 7 10 1 8]
[ 2 8 2 5 9]]
A'*A <type 'numpy.ndarray'>
[[ 4 56 20 54 4]
[ 56 9 7 42 80]
[ 20 7 9 100 2]
[ 54 42 100 1 40]
[ 4 80 2 40 81]]
推荐答案
Numpy documentation states that the operator you apply performs element-wise multiplication.
但是,MATLAB中的 mtimes
会进行矩阵乘法
However, mtimes
in MATLAB does matrix multiplication.
要进行验证,MATLAB逐元素乘法的语法会产生与numpy中相同的结果:
To verify, MATLAB syntax for element-wise multiplication produces the same result you see in numpy:
disp(x.'.*x)
4 56 20 54 4
56 9 7 42 80
20 7 9 100 2
54 42 100 1 40
4 80 2 40 81
这篇关于矩阵乘法导致MATLAB和NUMPY(?)中的值不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文