Each array has a natural (1D flattened) order for its elements. When you modify the array, it is as if first smoothed out (thus, a natural order is obtained), and then it changes:
In [54]: z.ravel() Out[54]: array([ 0, 3, 6, 1, 4, 7, 2, 5, 8, 9, 12, 15, 10, 13, 16, 11, 14, 17]) In [55]: z.ravel().reshape(2*3, 3) Out[55]: array([[ 0, 3, 6], [ 1, 4, 7], [ 2, 5, 8], [ 9, 12, 15], [10, 13, 16], [11, 14, 17]])
Note that in the "natural order" 0 and 1 are far apart. However, you change it, 0 and 1 will not be next to each other along the last axis, what you want in the desired array:
desired = np.array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14], [15, 16, 17]])
This requires some reordering, which in this case can be done using swapaxes :
In [53]: z.swapaxes(1,2).reshape(2*3, 3) Out[53]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14], [15, 16, 17]])
because swapaxes(1,2) puts the values ββin the desired order
In [56]: z.swapaxes(1,2).ravel() Out[56]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]) In [57]: desired.ravel() Out[57]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
Note that the reshape method also has an order parameter that can be used to control (C- or F-) the order by which the elements are read from the array and placed in the reconfigured array. However, I do not think this will help in your case.
Another way to think about reshape boundaries is to say that all changes followed by ravel are the same:
In [71]: z.reshape(3,3,2).ravel() Out[71]: array([ 0, 3, 6, 1, 4, 7, 2, 5, 8, 9, 12, 15, 10, 13, 16, 11, 14, 17]) In [72]: z.reshape(3,2,3).ravel() Out[72]: array([ 0, 3, 6, 1, 4, 7, 2, 5, 8, 9, 12, 15, 10, 13, 16, 11, 14, 17]) In [73]: z.reshape(3*2,3).ravel() Out[73]: array([ 0, 3, 6, 1, 4, 7, 2, 5, 8, 9, 12, 15, 10, 13, 16, 11, 14, 17]) In [74]: z.reshape(3*3,2).ravel() Out[74]: array([ 0, 3, 6, 1, 4, 7, 2, 5, 8, 9, 12, 15, 10, 13, 16, 11, 14, 17])
So, if the ravel of the desired array is different, there is no way to get it just for modification.
The same applies to changing the form with order='F' if you also use ravel with order='F' :
In [109]: z.reshape(2,3,3, order='F').ravel(order='F') Out[109]: array([ 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7, 16, 8, 17]) In [110]: z.reshape(2*3*3, order='F').ravel(order='F') Out[110]: array([ 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7, 16, 8, 17]) In [111]: z.reshape(2*3,3, order='F').ravel(order='F') Out[111]: array([ 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7, 16, 8, 17])
You can get the desired array using two forms:
In [83]: z.reshape(2, 3*3, order='F').reshape(2*3, 3) Out[83]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14], [15, 16, 17]])
but I stumbled upon it aimlessly.
If I do not completely understand your question, and x and y are givens (not z ), then you can get the desired array using row_stack instead of dstack :
In [88]: z = np.row_stack([x, y]) In [89]: z Out[89]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14], [15, 16, 17]])