![](/img/trans.png)
[英]How can I concatenate the following python code as it done in Matlab?
[英]How to plot the following Matlab code into python?
我的 MATLAB 代碼:
x=1:28:9996;
#y_test is 1x178 double array
padding=nan(1,179);
plot(x,[padding,y_test])
我試圖在 python 中做同樣的事情,但它不起作用。 為什么?
#python
x=np.arange(1,9996,28)
padding=np.full((179),np.nan)
plt.plot(x,[padding,y_test])
它顯示此錯誤:
ValueError: x and y must have same first dimension, but have shapes (1, 357) and (2,)
而形狀是y_test.shape, padding.shape,x.shape=>((1, 178), (1, 179), (1, 357))
謝謝!
[padding,y_test]
在 Python 和 MATLAB 中不做同樣的事情。在 MATLAB 中,它沿着第一維連接兩個 arrays。 在 Python 中,它創建了一個包含兩個 arrays 作為其兩個元素的列表。
要連接兩個 NumPy arrays,請使用np.concatenate
、 np.stack
或column_stack
。
在您的情況下,您想執行np.concatenate((padding, y_test))
,假設padding
和y_test
是 1D arrays(如您的代碼生成的那樣)。 如果它們是形狀為 1xN 的 2D arrays(如您在評論中所述),則指定您要沿二維連接: np.concatenate((padding, y_test), axis=1)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.