Matlab到Python:为什么我们得到错误

时间:2015-01-08 14:48:39

标签: python numpy

它适用于MATLAB / OCTAVE - 如何在Python中正确修复它:

octave:40> whos YDFA_ale_ase
Variables in the current scope:

   Attr Name              Size                     Bytes  Class
   ==== ====              ====                     =====  ===== 
        YDFA_ale_ase     51x1                        408  double

Total is 51 elements using 408 bytes

octave:41> whos N1
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        N1          1x1200                    9600  double

Total is 1200 elements using 9600 bytes

octave:45> YDFA_ale_ase * N1
ans =

 Columns 1 through 20:

   46.8270   46.8270   46.8270 
..........................

但在Python中我得到以下错误:

np.dot(YDFA_ale_ase, 1.-N1)-np.dot(YDFA_ala_ase, N1)

ValueError: matrices are not aligned

其他尝试:

YDFA_ale_ase* 1.-N1-YDFA_ala_ase* N1

ValueError: operands could not be broadcast together with shapes (51) (1,1200) 

但是:

print YDFA_ale_ase.shape, N1.shape

给我

(51,) (1, 1200)

1 个答案:

答案 0 :(得分:1)

打印阵列的形状。请记住,Matlab矩阵至少有2个dims,而NUMPY可以是1或甚至0.很可能你试图dot (n,) (1,m)。在( n,)数组的右侧添加维度。 Y[:, None]是最简单的方法。

http://wiki.scipy.org/NumPy_for_Matlab_Users