Python or Numpy Approach to MATLAB's "cellfun"
<p>Is there a python or numpy approach similar to MATLABs "cellfun"? I want to apply a function to an object which is a MATLAB cell array with ~300k cells of different lengths.  </p>

<p>A very simple example:</p>

<pre><code>&gt;&gt;&gt; xx = [(4,2), (1,2,3)]
&gt;&gt;&gt; yy = np.exp(xx)

Traceback (most recent call last):
File "&lt;pyshell#47&gt;", line 1, in &lt;module&gt;
yy = np.exp(xx)
AttributeError: 'tuple' object has no attribute 'exp'
</code></pre>

<p>MATLAB cells were it's attempt to handle general lists like a real language.  But being MATLAB they have to be 2d.  But in general, in Python uses lists where MATLAB uses cells.  <code>numpy</code> arrays with <code>dtype=object</code> behave similarly, adding multidimensions.</p>

<p>Taking the object array route, I can use <code>frompyfunc</code> to apply this function to elements of a list or array:</p>

<pre><code>In [231]: np.frompyfunc(np.exp,1,1)([(4,2),(1,2,3)])
Out[231]: 
array([array([ 54.59815003,   7.3890561 ]),
       array([  2.71828183,   7.3890561 ,  20.08553692])], dtype=object)
In [232]: np.frompyfunc(np.exp,1,1)([(4,2),(1,2)])
Out[232]: 
array([[54.598150033144236, 7.3890560989306504],
       [2.7182818284590451, 7.3890560989306504]], dtype=object)
</code></pre>

<p>In the 2nd case the result is (2,2), in the first (2,) shape.  That's because of how <code>np.array([...])</code> handles those 2 inputs.</p>

<p>List comprehensions are just as fast, and probably give better control.  Or at least can be more predictable.</p>

python numpy elementwise-operations