# Mathematica permutes indices when reading Matlab files

I wrote the following Matlab script:

A=reshape(1:(3*4*5),[3,4,5]);
size(A)
save('A.mat','A');

B=reshape(1:(7*8),[7,8]);
size(B)
save('B.mat','B');

C=reshape(1:(2*3*4*5),[2,3,4,5]);
size(C)
save('C.mat','C');


and it ouput

ans =
3     4     5
ans =
7     8
ans =
2     3     4     5


which shows that indices go increasing.

Then I read these tensors with Mathematica:

In:= mat = Import["A.mat", "LabeledData"];
In:= Dimensions["A" /. mat]
Out= {5, 3, 4}
In:= mat = Import["B.mat", "LabeledData"];
In:= Dimensions["B" /. mat]
Out= {7, 8}
In:= mat = Import["C.mat", "LabeledData"];
In:= Dimensions["C" /. mat]
Out= {5, 4, 2, 3}


which shows that first two Matlab indices go to tail in the same order and remaining indices go from the beginning in reverse order.

Is this behaviour by design or by bug? Can I read somewhere about exact rules of index transposing during import?

I see no word about it here.

• matlabs arrays are odd. Fundamentally there are only 1- and 2- d arrays, with the higher order arrays implemented as arrays of arrays. Matlab arrays are fortran-like column major so the importer needs to do some transposition. That is to say there is some sense to what you see, but that doesn't make it right. Oct 19, 2017 at 17:52
• Maybe try hdf5 format? I've used python and octave to export complex high-dimensional matrices the let mathematica read in them without issue. matlab has a hdf5 write function mathworks.com/help/matlab/ref/hdf5write.html Oct 19, 2017 at 18:13

My personal opinion is that this is a bug.

Our MATLink package handles this well.

In:= << MATLink

In:= OpenMATLAB[]

In:= MEvaluate[
"a=reshape(1:(3*4*5), [3,4,5]);
size(a)"]

Out= ">>
ans =

3     4     5

"

In:= Dimensions@MGet["a"]

Out= {3, 4, 5}


Update:

I looked at the implementation of the MAT5 file importer. It is written in pure Mathematica. The relevant section is this: It gives the impression that this is done quite deliberately. Therefore I no longer think that it is a bug, but I do not know why it is done this way.

If you want to transfer data to/from MATLAB, and have both Mathematica and MATLAB installed on the same computer, I still recommend using MATLink.

• It still can be a bug, as author could erroneously think that Matlab has a problem with first pair of indices.
– Dims
Oct 19, 2017 at 21:07
• @Dims I think it does make sense in a way because MATLAB often presents 3D arrays (conceptually) as a "list of pages/slices" where each page is a matrix. That's also how it displays them. The way Import handles the data conforms with that view: we get a list of the same matrices in Mathematica as we saw at the MATLAB command line. Except that MATLAB uses the last index for the pages while Mathematica uses the first. Oct 20, 2017 at 16:34
• @Dims Of course once one starts looking at higher dimensional arrays, this does not generalize very well. That's why we did not make this choice in MATLink (but that was a long time ago and I forgot most of the discussions). I guess the most intuitive index handling depends on the specific application. I prefer MATLink's. Oct 20, 2017 at 16:35

as a workaround use hdf5

A=reshape(1:(3*4*5),[3,4,5]);
h5create('xyz.hdf5','/A',[3,4,5])
h5write('xyz.hdf5','/A',A)


mathematica

a=Import["xyz.hdf5", "Data"][]


this is fully reverse indexed..

Dimensions[a]


{5,4,3}

reverse the index order to be matlab-like:

a = Transpose[a, Reverse@Range@ArrayDepth@a]];
Dimensions[a]
`

{3,4,5}