Reshaping a numpy.array in Fortran-contiguous order

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

I have a array like following,

from numpy import * a=array([1,2,3,4,5,6,7,8,9])

I want to get the result like following

[[1,4,7],[2,5,8],[3,6,9]]

Because I have a big array. So i need a efficient way to do it . And it's better to reshape it in-place.

回答1:

As proposed by @atomh33ls, you can use a reshape passing order='F', and "if possible" the returned array will be only a view of the original one, without data being copied, for example:

a=array([1,2,3,4,5,6,7,8,9]) b = a.reshape(3,3, order='F')  a[0] = 11  print b #array([[ 1,  4,  7], #       [ 2,  5,  8], #       [ 3,  6,  9]])


回答2:

You can use reshape and change the order parameter to FORTRAN (column-major) order:

 a.reshape((3,3),order='F')


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!