Matlab to Python : why we are getting the error
Question:
It works in MATLAB / OCTAVE – how can I rightly fix it in 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
..........................
But in Python I get the below error:
np.dot(YDFA_ale_ase, 1.-N1)-np.dot(YDFA_ala_ase, N1)
ValueError: matrices are not aligned
Other attempt:
YDFA_ale_ase* 1.-N1-YDFA_ala_ase* N1
ValueError: operands could not be broadcast together with shapes (51) (1,1200)
However :
print YDFA_ale_ase.shape, N1.shape
Gives me
(51,) (1, 1200)
Answers:
Print the shape of your arrays. Keep in mind that Matlab matrices have at least 2 dims, while NUMPY can be 1 or even 0. Most likely you are trying to dot
a (n,)
with a (1,m)
. Add a dimension on thr right of your ( n,)
array. Y[:, None]
is the easiest way.
It works in MATLAB / OCTAVE – how can I rightly fix it in 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
..........................
But in Python I get the below error:
np.dot(YDFA_ale_ase, 1.-N1)-np.dot(YDFA_ala_ase, N1)
ValueError: matrices are not aligned
Other attempt:
YDFA_ale_ase* 1.-N1-YDFA_ala_ase* N1
ValueError: operands could not be broadcast together with shapes (51) (1,1200)
However :
print YDFA_ale_ase.shape, N1.shape
Gives me
(51,) (1, 1200)
Print the shape of your arrays. Keep in mind that Matlab matrices have at least 2 dims, while NUMPY can be 1 or even 0. Most likely you are trying to dot
a (n,)
with a (1,m)
. Add a dimension on thr right of your ( n,)
array. Y[:, None]
is the easiest way.