Modifying numpy.array form in Fortran contiguous order

I have an array like the following,

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

I want to get the result as follows

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

Because I have a large array. So I need an effective way to do this. And it’s better to change it in place.

+4
source share
2 answers

As suggested by @ atomh33ls, you can use the transition transfer order='F' , and, if possible, the returned array will only be a representation of the original, without copying the data, 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]]) 
+5
source

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

  a.reshape((3,3),order='F') 
+4
source

Source: https://habr.com/ru/post/1499130/


All Articles