Matlab 到 Python:为什么我们会收到错误
Matlab to Python : why we are getting the error
它在 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)
打印数组的形状。请记住,Matlab 矩阵至少有 2 个 dims,而 NUMPY 可以是 1 甚至 0。很可能你正在尝试 dot
a (n,)
with a (1,m)
。在 ( n,)
数组的右侧添加一个维度。 Y[:, None]
是最简单的方法。
它在 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)
打印数组的形状。请记住,Matlab 矩阵至少有 2 个 dims,而 NUMPY 可以是 1 甚至 0。很可能你正在尝试 dot
a (n,)
with a (1,m)
。在 ( n,)
数组的右侧添加一个维度。 Y[:, None]
是最简单的方法。