Matrix multiplication resulting in different values in MATLAB and NUMPY(?) [duplicate]

旧城冷巷雨未停 提交于 2019-12-01 22:58:41

问题


Here's the matrix

>> 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 gives me

>> 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

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

produces

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]]


回答1:


Numpy documentation states that the operator you apply performs element-wise multiplication.

However, mtimes in MATLAB does matrix multiplication.

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


来源:https://stackoverflow.com/questions/34028059/matrix-multiplication-resulting-in-different-values-in-matlab-and-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!