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

Converting a 3D matrix into vertically cascaded 1D arrays

下面这段代码中的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,))

您可以将 transpose or swapaxes 与一些 reshaping -

一起使用
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)