[SciPy-dev] Matlab io bug; request for advice

Matthew Brett matthew.brett at gmail.com
Wed Feb 18 22:26:01 EST 2009


Hi,

I've just found another bug in the matlab io (mine of course).  It's
easy to fix, but I'd like some advice about a change in undocumented
and somewhat unexpected behavior of the mat file writing.

The bug is, that when writing an array to a matlab matrix variable, I
had been appending 0s to the shape, for less than 2d arrays.

For 0d arrays (scalars), this results in shape (0,0), which is read in
matlab as an empty array.

That's obviously a bug, and easy to fix.  However, when passing in 1d
arrays, it results in shapes like (3,0).  Although this shape is
illegal in matlab, it seems to kindly just change the 0 to a 1,
resulting in a column vector.

However, matlab tends to think of an unshaped vector as being a row vector:

>> a = 1:12;
>> size(a)

ans =

     1    12

And numpy thinks the same:

>>> print np.atleast_2d(np.array([1,2])).shape
(1, 2)

It seems to me then, that I should assume the same, that a 1
dimensional array is a row vector.   However, this will change the
matlab shape of a 1 d array passed into the matlab mat file routines
from a column vector to a row vector.

Do y'all think I should:

a) Given this is undocumented anyway, just switch to the row vector
b) Keep 1d arrays as column vectors
c) Raise a warning and change for a future release

?

Thanks,

Matthew



More information about the SciPy-Dev mailing list