vvy*_*vvy 3 matlab numpy matrix-multiplication
这是矩阵
>> 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]
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
但是,python(numpy)中的相同操作(在相同数据上)会产生不同的结果.我无法理解为什么?
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
Run Code Online (Sandbox Code Playgroud)
产生
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]]
Run Code Online (Sandbox Code Playgroud)
Numpy 文档声明您应用的运算符执行逐元素乘法.
但是,mtimes在MATLAB中进行矩阵乘法.
要验证,元素乘法的MATLAB语法会产生与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
Run Code Online (Sandbox Code Playgroud)