简体   繁体   中英

Matrix gives in Python other norm value than in Matlab

My goal is to use the matrix defined below in Matlab in my Python code but apparently the objects don't have the same norm? Thus, I believe that I made a mistake.

Tforward = np.array(np.mat('0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274; \
                   0.219417649252501   0.449283757993216   0.449283757993216   0.219417649252501  -0.219417649252501  -0.449283757993216  -0.449283757993216  -0.219417649252501; \
                   0.569359398342846   0.402347308162278  -0.402347308162278  -0.569359398342846  -0.083506045090284   0.083506045090284  -0.083506045090284   0.083506045090284; \
                  -0.083506045090284   0.083506045090284  -0.083506045090284   0.083506045090284   0.569359398342846   0.402347308162278  -0.402347308162278  -0.569359398342846; \
                   0.707106781186547  -0.707106781186547                   0                   0                   0                   0                   0                   0; \
                   0                   0   0.707106781186547  -0.707106781186547                   0                   0                   0                   0; \
                   0                   0                   0                   0   0.707106781186547  -0.707106781186547                   0                   0; \
                   0                   0                   0                   0                   0                   0   0.707106781186547  -0.707106781186547'))

sum(Tforward**2,2)

>>> array([3.00428749, 2.99571251, 2.99571251, 3.00428749, 3.00428749,
       2.99571251, 2.99571251, 3.00428749])
Tforward =  [ 0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274   0.353553390593274;
       0.219417649252501   0.449283757993216   0.449283757993216   0.219417649252501  -0.219417649252501  -0.449283757993216  -0.449283757993216  -0.219417649252501;
       0.569359398342846   0.402347308162278  -0.402347308162278  -0.569359398342846  -0.083506045090284   0.083506045090284  -0.083506045090284   0.083506045090284;
      -0.083506045090284   0.083506045090284  -0.083506045090284   0.083506045090284   0.569359398342846   0.402347308162278  -0.402347308162278  -0.569359398342846;
       0.707106781186547  -0.707106781186547                   0                   0                   0                   0                   0                   0;
                       0                   0   0.707106781186547  -0.707106781186547                   0                   0                   0                   0;
                       0                   0                   0                   0   0.707106781186547  -0.707106781186547                   0                   0;
                       0                   0                   0                   0                   0                   0   0.707106781186547  -0.707106781186547];   

sum(Tforward.^2,2)

>>> ans =

   1.00000
   1.00000
   1.00000
   1.00000
   1.00000
   1.00000
   1.00000
   1.00000

Any help appreciated

The difference between MATLAB Sum and Python Sum is not the same thing. If you have MATRIX A, sum(A,2) in Matlab gives you the sum from the second column. However, in Python, sum(A,2) gives you the sum from of the list but also applies the number you entered into the list.

So in MATLAB you did sum(A(:,2)) while in Python you did sum(A+2). I believe Taha is correct in the comments that you want np.sum instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM