cellfun will apply your function to the contents of you cell array. There are 3 ways to specify the function: as a char array (only a handfull of functions, but they are faster when called like this)\ with a function handle, e.g. @mean with an anonymous function That last option is what you see here. %instead of this function output=MyFun(in1,in2) output=in1.*in2; end %you do MyFun=@(in1,in2) in1.*in2; Answer from Rik on mathworks.com
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › cell arrays
cellfun - Apply function to each cell in cell array - MATLAB
A = cellfun(___,Name,Value) applies func with additional options specified by one or more Name,Value pair arguments. For example, to return output values in a cell array, specify 'UniformOutput',false. You can return A as a cell array when func returns values that cannot be concatenated into ...
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › tables
varfun - Apply function to table or timetable variables - MATLAB
In MATLAB, such variables have sizes of 0-by-0. Refer to the usage notes and limitations in the C/C++ Code Generation section. The same usage notes and limitations apply to GPU code generation. ... To return an output whose data type matches the data type of the input, specify the OutputFormat name-value argument as "auto". This value is the default value. rowfun | cellfun | structfun | arrayfun | findgroups | splitapply | groupsummary | convertvars | vartype
Discussions

If function with cellfun (i.e. vectorized code instead of ridiculously slow loop).
Hello everyone. I am new to matlab and really try to optimize my code since it takes to much time to run. I don't really understand the concept behind cellfun yet. If we take the following exampl... More on mathworks.com
🌐 mathworks.com
0
0
March 14, 2018
Replacing character withing an cell using cellfun
I have a cell array called "ImpactTime" within a table called "hits" (see figure below). I'm attempting to change the 6th character in ImpactTime from a "." to a ":". I'm aware I can do this with ... More on mathworks.com
🌐 mathworks.com
1
0
June 5, 2018
speeding up using cellfunction and arrayfun versus for-loop
speeding up using cellfunction and arrayfun... Learn more about arrayfun, cellfun, for loop MATLAB More on mathworks.com
🌐 mathworks.com
1
0
July 6, 2021
Cellfun or For loop
Tell us once you tried it :) More on reddit.com
🌐 r/matlab
6
3
September 11, 2019
🌐
MathWorks
mathworks.com › matlabcentral › answers › 388205-if-function-with-cellfun-i-e-vectorized-code-instead-of-ridiculously-slow-loop
If function with cellfun (i.e. vectorized code instead of ridiculously slow loop). - MATLAB Answers - MATLAB Central
March 14, 2018 - Using cellfun isn't the same as vectorizing and won't make things faster. It just hides the for-loop. Your loops over k can be vectorized, but to help us show you how, you should replace the image of your code with actual text that we can copy/paste. ... https://www.mathworks.com/matlabcentral/answers/388205-if-function-with-cellfun-i-e-vectorized-code-instead-of-ridiculously-slow-loop#comment_545320
🌐
MathWorks
mathworks.com › matlabcentral › answers › 873328-speeding-up-using-cellfunction-and-arrayfun-versus-for-loop
speeding up using cellfunction and arrayfun versus for-loop - MATLAB Answers - MATLAB Central
July 6, 2021 - You code seems to have a great potential for speed improvements. So a comparison of cellfun, arrayfun or loops is not really smart yet. But it is expected, that loops are faster: cellfun and arrayfun are mex functions, which have to call the Matlab level for each element.
Find elsewhere
🌐
MathWorks
mathworks.com › matlabcentral › answers › 169166-cellfun-with-empty-cell-input
cellfun with empty cell input - MATLAB Answers - MATLAB Central
January 7, 2015 - When I use cellfun with an empty cell as input, the output is an empty array. Is that a bug in matlab? How can that be avoided? If i do x = arrayfun(@(i){y(i)},t), and t is not an empty arra...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 769592-different-outcome-cellfun-isempty-vs-cellfun-isempty
different outcome cellfun(@isempty,..) vs cellfun('isempty',...) - MATLAB Answers - MATLAB Central
March 11, 2021 - Further I tried to do this with cellfun('isempty',mycell) (according to what I've read this should be faster). However the output is not the same (the last one returns a logical vector with all logical zero's). I've checked the documentation but could not figure out why the outcomes differ. Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/769592-different-outcome-cellfun-isempty-vs-cellfun-isempty#answer_645087
Top answer
1 of 2
5

The most readable/maintainable approach will probably be to use a list comprehension:

yy = [ np.exp(xxi) for xxi in xx ]

That relies on numpy.exp to implicitly convert each tuple into a numpy.ndarray, which in turn means that you'll get a list of numpy.ndarrays 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:

yy = [ tuple(np.exp(xxi)) for xxi in xx ]

For some purposes (e.g. to avoid memory bottlenecks) you may prefer to use a generator expression rather than a list comprehension (round brackets instead of square).

2 of 2
2

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. numpy arrays with dtype=object behave similarly, adding multidimensions.

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

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)

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

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

🌐
MathWorks
mathworks.com › matlabcentral › answers › 375731-cellfun-for-function-with-multiple-inputs
cellfun for function with multiple inputs - MATLAB Answers - MATLAB Central
January 5, 2018 - 'Later' in your case means in the cellfun statement, where each element of the cell array will successively be given to that function as the 'a' argument. ... https://www.mathworks.com/matlabcentral/answers/375731-cellfun-for-function-with-multiple-inputs#comment_521794