[SciPy-User] Simple ndarray dim question?

Erik Kastman erik.kastman@gmail....
Thu May 10 16:15:43 CDT 2012

Hi all,

Using SciPy and Matlab, I'm having trouble reconstructing an array to match what is given from a matlab cell array loaded using scipy.io.loadmat().

For example, say I create a cell containing a pair of double arrays in matlab and then load it using scipy.io (I'm using SPM to do imaging analyses in conjunction with pynifti and the like)


    >> onsets{1} = [0 30 60 90]
    >> onsets{2} = [15 45 75 105]


    >>> import scipy.io as scio
    >>> mat = scio.loadmat('onsets.mat')
    >>> mat['onsets'][0]
    array([[[ 0 30 60 90]], [[ 15  45  75 105]]], dtype=object)
    >>> mat['onsets'][0].shape

My question is this: **Why does this numpy array have the shape (2,) instead of (2,1,4)**? In real life I'm trying to use Python to parse a logfile and build these onsets cell arrays, so I'd like to be able to build them from scratch.

When I try to build the same array from the printed output, I get a different shape back:

    >>> new_onsets = array([[[ 0, 30, 60, 90]], [[ 15,  45,  75, 105]]], dtype=object)
    array([[[0, 30, 60, 90]],
           [[15, 45, 75, 105]]], dtype=object)
    >>> new_onsets.shape

Unfortunately, the shape (vectors of doubles in a cell array) is coded in a spec upstream, so I need to be able to get this saved exactly in this format. Of course, it's not a big deal since I could just write the parser in matlab, but it would be nice to figure out what's going on and add a little to my [minuscule] knowledge of numpy.

Thanks in advance for any suggestions,

cross-posted to stack-overflow: http://stackoverflow.com/questions/10542263

More information about the SciPy-User mailing list