My MATLAB code:
x=1:28:9996;
#y_test is 1x178 double array
padding=nan(1,179);
plot(x,[padding,y_test])
I am trying to do the same in python but it is not working. why?
#python
x=np.arange(1,9996,28)
padding=np.full((179),np.nan)
plt.plot(x,[padding,y_test])
It show this error:
ValueError: x and y must have same first dimension, but have shapes (1, 357) and (2,)
while the shape is y_test.shape, padding.shape,x.shape=>((1, 178), (1, 179), (1, 357))
thank you!
[padding,y_test]
does not do the same thing in Python and in MATLAB. In MATLAB it concatenates the two arrays along the 1st dimension. In Python it creates a list containing the two arrays as its two elements.
To concatenate two NumPy arrays, use np.concatenate
, np.stack
, or column_stack
.
In your case, you want to do np.concatenate((padding, y_test))
, assuming padding
and y_test
are 1D arrays (as your code generates). If they are 2D arrays with shape 1xN (as you claim in a comment), then specify you want to concatenate along the 2nd dimension: np.concatenate((padding, y_test), axis=1)
.
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.