## Get data sequentially from numpy array in Matlab ordering

Question

As an example, suppose, in Matlab, a Matrix `a(2,3,2)`

like this:

```
a(:,:,1) =
1 2 3
4 5 6
a(:,:,2) =
7 8 9
10 11 12
```

If I use `mex`

and access the elements of this matrix sequentially, I get the following order (in the end, a code to access them sequentially):

```
1, 4, 2, 5, 3, 6, 7, 10, 8, 11, 9, 12
```

Now, if I have the same matrix in numpy

```
In [2]: np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
Out[2]:
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
```

, I could try to use `.flatten(...)`

to access the items sequentially (couldn't find a better way to do it -- I am open to suggestions). `flatten()`

has 4 "ordering" options:

```
In [4]: a.flatten('F')
Out[4]: array([ 1, 7, 4, 10, 2, 8, 5, 11, 3, 9, 6, 12])
In [5]: a.flatten('C')
Out[5]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
In [6]: a.flatten('A')
Out[6]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
In [7]: a.flatten('K')
Out[7]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
```

Is there an elegant way to access the elements of the numpy array in a Matlab ordering? (in my real use case, these matrix are big, so copiyng them around wouldn't be preferred)

## Appendix: Code to print the matrix sequentially

[not great, I know, just for testing]

```
1 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
2 {
3 // This just a test: hardcoding the size of `data_input`
4 int data_input_size = 12;
5 double *data_input;
6 const mxArray *batch_data;
7
8 // Gets a pointer to the first element of batch_data
9 data_input = mxGetPr(prhs[0]);
10
11 for(int i = 0; i < data_input_size; i++) {
12 printf("working_data[i]: %f\n", data_input[i]);
13 }
14 plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
15 }
```

Show source

## Answers ( 2 )

This bugs a lot of people going into NumPy/Python from MATLAB. So, in MATLAB, the indexing format is

`(column x row x dim3)`

and so on. With NumPy, it's`(axis-0, axis-1, axis-2)`

and so on.To show this schematically using a sample case on MATLAB :

On NumPy :

Let's try to correlate these dimensions and axes terminology for the

`3D`

array case listed in the question between these twoenvironments:Thus, to simulate the same behavior in NumPy as MATLAB, we need the axes in NumPy as :

`(1,2,0)`

. Together with NumPy's way of storing elements starting from the last axis to the first one i.e. in reversed order, the required axes order would be`(0,2,1)`

.To perform the permuting of axes that way, we could use

`np.transpose`

and thereafter use a flattening operation with`np.ravel()`

-Sample run -

This is intended as a suplement to

`@Divakar's`

answer.In Octave I made

`x`

and`a`

and saved them to a fileI can flatten

`a`

with (same as your`mex`

):In numpy, with

`loadmat`

from`scipy.io`

`ravel`

with order 'F' displays the original flat`x`

:To help keep track of shapes I prefer to make them all different. For example

and

`a`

displays asIt is showing 4 (2,3) matrices.

On the numpy side I get:

This is 2 blocks of (3,4) shape.

I see two key differences between numpy and MATLAB/Octave. Default order is different, but

`numpy`

lets you specify`F`

. And display of 3d (and larger) arrays is different. There's in effect a different notion of 'planes' or blocks.`np.transpose`

changes the shape, strides, and order of an array, while keeping the same data base.Applied to

`a`

, it produces a (4,3,2) array with order 'C':The MATLAB display can be replicated with: