Explanation of cellfun()

조회 수: 401 (최근 30일)
Marvin Eckert
Marvin Eckert 2020년 5월 20일
댓글: Marvin Eckert 2021년 11월 25일
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?

채택된 답변

Rik
Rik 2020년 5월 20일
cellfun will apply your function to the contents of you cell array. There are 3 ways to specify the function:
  1. as a char array (only a handfull of functions, but they are faster when called like this)\
  2. with a function handle, e.g. @mean
  3. 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
Rik
Rik 2021년 11월 24일
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
Marvin Eckert 2021년 11월 25일
Hi Rik,
very cool. Thanks for your comment much appreciated.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by