301 views (last 30 days)
Show older comments
Marvin Eckert on 20 May 2020
Commented: Marvin Eckert on 25 Nov 2021
Accepted Answer: Rik
Open in MATLAB Online
Explaination of cellfun() call with @(data):
Hi everyone,
I have a rather stupid question I think, but I do not understand a specific call of the cellfun().
My question came up when I was working with the MATLAB example: Similarity-Based Remaining Useful Life Estimation. There is a function applied to each cell of a cell array, for example in line 34:
trainDataNormalized = cellfun(@(data) regimeNormalization(data, centers, centerstats), ...
trainData, 'UniformOutput', false);
[...] line 176: (for info)
function data = regimeNormalization(data, centers, centerstats)
For me the content of the cellfun() and its function is clear, exept of the expression @(data). Cellfun() applies the function regimeNormalization individual to each cell of the cell array trainData.
Looking in the doku of cellfun() they call a funktion like this and leave out the additional function like above. Which I think I undestand, see below.
A = cellfun(@mean,C)
p = cellfun(@plot,X,Y);
But in the documentation they also do this, which is exactly like my problem, but the explaination is not sufficient for me. (MATLAB advanced beginner) What is the expression @(x) mean?
B = cellfun(@(x) x(1:3),str,'UniformOutput',false)
Does cellfun() accessing one cell of the cell array temporarily store the data inside the cell in the variable x?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Rik on 20 May 2020
Open in MATLAB Online
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;
4 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Marvin Eckert on 20 May 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_851678
Edited: Marvin Eckert on 20 May 2020
Open in MATLAB Online
Tanks for your quick answer. :)
Your answer were helpflull, because I did not thought about anonymous functions. I think I undestand now what the cellfun()
trainDataNormalized = cellfun(@(data) regimeNormalization(data, centers, centerstats), ...
trainData, 'UniformOutput', false);
above is doing.
To be shure I explain it in my words and you give a short YES or NO? That would be very helpful to me.
The function temporarily stores the information within a single cell of the trainData cell array in the variable data, executes the regimeNormalization() function using the variable data as input, and writes the result of regimeNormalization() function back to this cell.
John Navarro on 24 Nov 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1850669
Hello. I think the answer is NO. cellfun is required because of the format or the variable, or the data type. So far that I understand is that regimenNormalization() is being evaluated for each cell of the variable (data).
Chech this for more details
https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
https://www.mathworks.com/help/matlab/ref/cellfun.html?s_tid=doc_ta
Rik on 24 Nov 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1850679
Open in MATLAB Online
Actually the answer is yes.
Cellfun in this case is only hiding the loop.
%the first step is to create an anonymous function with the input variable
%called data. The values of the variables centers and centerstats are
%captured with their current value.
anon_function=@(data) regimeNormalization(data, centers, centerstats);
%the second step is to execute the anonymous function for each cell and
%store the result in a cell array.
trainDataNormalized = cellfun(anon_function, ...
trainData, 'UniformOutput', false);
The cellfun call is equivalent to this:
trainDataNormalized=cell(size(trainData));
for n=1:numel(trainData)
trainDataNormalized{n}=anon_function(trainData{n});
end
The loop will probably be marginally faster.
It is the anonymous function that is evaluated for every cell. Inside that anonymous function there is a call to regimenNormalization, which is called with the data in each cell, but also with two other variables that were captured when the anonymous function was created.
Marvin Eckert on 25 Nov 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/527468-explanation-of-cellfun#comment_1852714
Hi Rik,
very cool. Thanks for your comment much appreciated.
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office