I have a numpy array that I need to change the order of the axis.
To do that I am using moveaxis()
method, which only returns a view of the input array, by changing only the strides of the array.
However, this does not change the order that the data are stored in the memory. This is problematic for me because I need to pass this reorderd array to a C code in which the order that the data are stored matters.
import numpy as np
a=np.arange(12).reshape((3,4))
a=np.moveaxis(a,1,0)
In this example, a
is originally stored continuously in the memory as [0,1,2,...,11]
.
I would like to have it stored [0,4,8,1,5,9,2,6,10,3,7,11]
, and obviously moveaxis()
did not do the trick
How could I force numpy to rewrite the array in the memory the way I want? I precise that contrary to my simple example, I am manipulating 3D or 4D data, so I cannot simply change the ordering from row to col major when I create it.
Thanks!