@(x)sum(x.IsBranch)
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
![1.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200720/1.png)
Each cell is an independent regression tree containing all the properties shown below.
![2.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200721/2.png)
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.
댓글 수: 0
채택된 답변
Guillaume
2019년 1월 14일
편집: Guillaume
2019년 1월 14일
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.
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!