## @(x)sum(x.IsBranch)

on 14 Jan 2019
Latest activity Edited by Guillaume

on 14 Jan 2019

### Guillaume (view profile)

Hi,
As I tried to understand the example line by line, I am stuck at below:
numBranches = @(x)sum(x.IsBranch);
mdlDefaultNumSplits = cellfun(numBranches, MdlDefault.Trained);
To my understanding, MdlDefault.Trained is a 10x1 cell (10-fold). Shown below. Each cell is an independent regression tree containing all the properties shown below. from the above two lines of codes, function @(x)sum(x.IsBranch) has an x in the function, to my understanding here x is MdlDefault.Trained. However, MdlDefault.Trained has so many properties. This function would look like:
(MdlDefault.Trained)sum(MdlDefault.Trained.IsBranch).
What does above line mean? I feel it is difficult to understand.

### Guillaume (view profile)

on 14 Jan 2019
Edited by Guillaume

### Guillaume (view profile)

on 14 Jan 2019

cellfun is just a convenient way to iterate over all the elements of a cell array. It pass each element in turn to the function it is given. In your particular case, the function is an anonymous function with one input (called x, the name doesn't matter) which returns the sum of x.IsBranch. If we were to deconstruct that cellfun line, it would be equivalent to:
mdlDefaultNumSplits = zeros(size(MdlDefault.Trained)); %cellfun automatically create the output array
for iter = 1:numel(mdlDefaultNumSplits) %cellfun iterates over each element
x = MdlDefault.Trained{iter}; %and dereference the cell content
%then it calls the function which in this case does
z = sum(x.IsBranch);
%and put the output of the anonymous function in the output matrix:
mdlDefaultNumSplits(iter) = z;
end
So x is the content of a single cell of MdlDefault.Trained which according to your first screenshot is a CompactRegressionTree. I don't have the stats toolbox so can check what IsBranch is. Since it's not listed as a property, I assume it's a function of CompactRegressionTree. Probably one that returns a logical array.