简体   繁体   English

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

[英]Converting a 3D matrix into vertically cascaded 1D arrays

Is there any shortcut to the for loop in the following piece of code?下面这段代码中是否有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,))

You can use transpose or swapaxes with some reshaping -您可以使用transposeswapaxes进行一些reshaping -

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

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

You can replace the ravel with reshape -您可以用reshape替换ravel -

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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