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>The most readable/maintainable approach will probably be to use a <a href="https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a>:</p>

<pre><code>yy = [ np.exp(xxi) for xxi in xx ]
</code></pre>

<p>That relies on <code>numpy.exp</code> to implicitly convert each tuple into a <code>numpy.ndarray</code>, which in turn means that you'll get a list of <code>numpy.ndarray</code>s back rather than a list of tuples. That's probably OK for nearly all purposes, but if you absolutely have to have tuples that's also easy enough to arrange:</p>

<pre><code>yy = [ tuple(np.exp(xxi)) for xxi in xx ]
</code></pre>

<p>For some purposes (e.g. to avoid memory bottlenecks) you may prefer to use a <a href="https://docs.python.org/2/reference/expressions.html#generator-expressions" rel="nofollow">generator expression</a> rather than a list comprehension (round brackets instead of square).</p>

python numpy elementwise-operations