Finding number of all nested cells in a complex cell

前端 未结 2 1305
余生分开走
余生分开走 2020-12-20 08:12

I have a nested cell which represents a tree-structure:

CellArray={1,1,1,{1,1,1,{1,1,{1,{1 1 1 1 1 1 1 1}, 1,1},1,1},1,1,1},1,1,1,{1,1,1,1}};
2条回答
  •  离开以前
    2020-12-20 08:39

    Here's a simpler approach using a single while loop and repeated concatenation of the sub-cells:

    temp = CellArray;
    nNodes = 0;
    while iscell(temp)
      index = cellfun(@iscell, temp);
      nNodes = nNodes + sum(index);
      temp = [temp{index}];
    end
    

    And the result for the sample CellArray in the question:

    nNodes =
    
         5
    

提交回复
热议问题