2016-02-12 108 views
0

在這裏,我需要得到一個二維數組如何連接的陣列的兩個部分,使一個新的陣列

x = np.zeros((10, 4)) 
y = np.ones((10, 4)) 
c = np.array([x[0:3, :], y[0:3, :]]) 
print c.shape # I get (2, 3, 4) 
np.reshape(c, (6, 4)) 
print c.shape # I get (2, 3, 4) 

我需要4列得到的6行的二維數組。

+1

有沒有嘗試過任何各種[陣列組合方法](https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html#joining-arrays)? – wflynny

+1

'np.reshape()'不會改變對象的位置。它返回數組的新視圖,但是你的代碼只是忽略了返回值。 –

回答

2
np.concatenate((x[0:3,:], y[0:3,:]), axis=0) 

或者

np.vstack((x[0:3,:],y[0:3,:])) 
1

最簡潔的解決方案可能是

c = np.r_[x[:3], y[:3]] 

(最簡潔的解決方案不一定是最可讀的解決方案。)