将3D矩阵转换为垂直级联的1D阵列

时间:2015-09-11 21:53:06

标签: python numpy matrix vectorization reshape

以下代码中是否有for循环的快捷方式?

import numpy as np

ff = np.zeros((3,3,4))
gg = np.zeros((36,1))

ff[:,:,0] = 1
ff[:,:,1] = 2
ff[:,:,2] = 3
ff[:,:,3] = 4


for ii in xrange(4):
    gg[ii*9:(ii+1)*9,0] = ff[:,:,ii].reshape((9,))

1 个答案:

答案 0 :(得分:2)

您可以将transposeswapaxesreshaping -

一起使用
ff.transpose([2,1,0]).ravel()[:,None]

ff.swapaxes(0,2).ravel()[:,None]

您可以将ravel替换为reshape -

ff.transpose([2,1,0]).reshape(-1,1)

ff.swapaxes(0,2).reshape(-1,1)