矩阵乘法的结果在Python和MATLAB

时间:2016-12-08 20:34:00

标签: python matlab

我遇到一个我无法理解的问题,这让我感到非常疯狂,因为我无法找到解决方案。

我在MATLAB和Python上做了一些矩阵乘法。想象一下,我有两个矩阵X和W,我想将它们相乘。 在Python中我使用numpy,我这样做: np.dot(X, W)

在MATLAB中我做:X*W

Python的结果就是这个:

[[ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -1.79812377e-01   1.26046711e-02  -3.62915515e-01  -2.28314197e-01
    9.41395740e-02   1.95587346e-01   4.00916792e-02   4.61162174e-01
   -1.54852385e-01   2.07742254e-01]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]
 [ -7.25949823e-04  -9.78931123e-04  -2.20816949e-05  -2.52954078e-03
   -2.53120361e-03  -3.53331962e-03  -3.62886737e-03  -4.73257530e-03
   -4.44088094e-05  -4.29659134e-03]]

MATLAB上的结果:

-0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

0.4886    0.4726    0.7100    0.9864    0.6025    0.5887    0.9668    0.4671    0.2921    0.9398

-0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

   -0.0007   -0.0010   -0.0000   -0.0025   -0.0025   -0.0035   -0.0036   -0.0047   -0.0000   -0.0043

我想知道为什么第二行不同。 XW矩阵位于以下位置:

PYTHON:

W = np.array([[ 0.16157533,0.17941953,0.11275408,0.4501205,     
0.38326338,0.49979055, 0.56796654,0.61752605,0.05109819,0.63738453],     
[0.51000276,0.35098523,0.81868132,0.92687111,0.38791804,0.29996999,     
0.70714705,0.00453668,0.34100865,0.55859484],     
[0.31635177,0.99422952,0.81529534,0.42029186,0.58907765,0.20727667,     
0.75791727,0.07188677,0.27872427,0.92982283]])

X 
[[ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [  7.38717964e-01  -6.55268545e-01   1.10692571e-01]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]
 [ -7.67758224e-03   9.73418978e-04   5.72517607e-05]]

MATLAB

W = [0.16157533 0.17941953 0.11275408 0.4501205 0.38326338 0.49979055 0.56796654 0.61752605 0.05109819 0.63738453;0.51000276 0.35098523 0.81868132 0.92687111 0.38791804 0.29996999 0.70714705 0.00453668 0.34100865 0.55859484; 0.31635177 0.99422952 0.81529534 0.42029186 0.58907765 0.20727667 0.75791727 0.07188677 0.27872427 0.92982283];

Xf = [-7.67758224e-03 9.73418978e-04 5.72517607e-05; 7.38717964e-01 6.55268545e-01 1.10692571e-01; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05; -7.67758224e-03 9.73418978e-04 5.72517607e-05];

1 个答案:

答案 0 :(得分:2)

在Python中,X的第七个值是-6.55268545e-01(否定)。

在Matlab中,xf的第七个值是6.55268545e-01(正数)。

可能还有其他差异,我在第一次发现时就停止了搜索。