简体   繁体   中英

Reshaping 4D numpy to 5D array using specific pattern

I have a 4D numpy array of shape (N x 8 x 24 x 98) that I need to reshape to the 5D shape (N x 8 x 24 x 7 x 14) such that the last dimension is split into 2 separate dimensions.

If v_i is the value for element i in the last dimension of the old matrix (containing 98 elements), then the values should be ordered as the following in the 2 new dimensions of shape 7 x 14 :

[[v_0, v_1, v_2, v_3, v_4, v_5, v_6], [v_7, v_8, v_9, v_10, v_11, v_12, v_13], ...]

Performance is not important so the solution could potentially use a for-loop if needed.

IIUC you can simply reshape your array / matrix:

In [109]: a = np.arange(8*24*98).reshape(8,24,98)

In [110]: a.shape
Out[110]: (8, 24, 98)

In [111]: x = a.reshape(8,24,7,14)

In [112]: x.shape
Out[112]: (8, 24, 7, 14)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM