I wrote the following Matlab script:




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[20]:= mat = Import["A.mat", "LabeledData"];
In[15]:= Dimensions["A" /. mat]
Out[15]= {5, 3, 4}
In[21]:= mat = Import["B.mat", "LabeledData"];
In[17]:= Dimensions["B" /. mat]
Out[17]= {7, 8}
In[22]:= mat = Import["C.mat", "LabeledData"];
In[23]:= Dimensions["C" /. mat]
Out[23]= {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.

  • $\begingroup$ 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. $\endgroup$
    – george2079
    Oct 19, 2017 at 17:52
  • $\begingroup$ 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 $\endgroup$ Oct 19, 2017 at 18:13

2 Answers 2


My personal opinion is that this is a bug.

Our MATLink package handles this well.

In[1]:= << MATLink`

In[2]:= OpenMATLAB[]

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

Out[3]= ">> 
ans =

     3     4     5


In[5]:= Dimensions@MGet["a"]

Out[5]= {3, 4, 5}


I looked at the implementation of the MAT5 file importer. It is written in pure Mathematica. The relevant section is this:

enter image description here

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.

  • $\begingroup$ It still can be a bug, as author could erroneously think that Matlab has a problem with first pair of indices. $\endgroup$
    – Dims
    Oct 19, 2017 at 21:07
  • $\begingroup$ @Dims Please contact Wolfram Support and let them know about this problem (then tell us what they said). $\endgroup$
    – Szabolcs
    Oct 19, 2017 at 22:09
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Oct 20, 2017 at 16:34
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Oct 20, 2017 at 16:35

as a workaround use hdf5



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

this is fully reverse indexed..



reverse the index order to be matlab-like:

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



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.