2
$\begingroup$

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[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.

$\endgroup$
  • $\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 '17 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$ – egwene sedai Oct 19 '17 at 18:13
3
$\begingroup$

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]);
 size(a)"]

Out[3]= ">> 
ans =

     3     4     5

"

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

Out[5]= {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:

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.

$\endgroup$
  • $\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 '17 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 '17 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 '17 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 '17 at 16:35
2
$\begingroup$

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"][[1]] 

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}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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