Note that there are some explanatory texts on larger screens.

plurals
  1. POndim in numpy array loaded with scipy.io.loadmat?
    primarykey
    data
    text
    <p>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().</p> <p>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)</p> <p>MATLAB</p> <pre><code>&gt;&gt; onsets{1} = [0 30 60 90] &gt;&gt; onsets{2} = [15 45 75 105] </code></pre> <p>Python</p> <pre><code>&gt;&gt;&gt; import scipy.io as scio &gt;&gt;&gt; mat = scio.loadmat('onsets.mat') &gt;&gt;&gt; mat['onsets'][0] array([[[ 0 30 60 90]], [[ 15 45 75 105]]], dtype=object) &gt;&gt;&gt; mat['onsets'][0].shape (2,) </code></pre> <p>My question is this: <strong>Why does this numpy array have the shape (2,) instead of (2,1,4)</strong>? 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.</p> <p>When I try to build the same array from the printed output, I get a different shape back:</p> <pre><code>&gt;&gt;&gt; new_onsets = array([[[ 0, 30, 60, 90]], [[ 15, 45, 75, 105]]], dtype=object) array([[[0, 30, 60, 90]], [[15, 45, 75, 105]]], dtype=object) &gt;&gt;&gt; new_onsets.shape (2,1,4) </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload