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}};
I want to find out the number of nodes in Matlab. I put a simple picture below that might help you understand what I am looking for more precisely:
Thanks.
If I understand correctly, you want the number of cell elements, that are themselves cells.
Then you can go recursively through your cell cells (and numbers) and check with iscell
to see which elements are cells. See the following, where totnod ultimately gives the number of nodes.
ind=cellfun(@iscell, Chains);
totnod=sum(ind);
oldtmp=Chains(ind);
while ~isempty(oldtmp)
newtmp={};
for i=1:length(oldtmp)
ind=cellfun(@iscell, oldtmp{i});
newtmp=[newtmp,oldtmp{i}(ind)];
totnod=totnod+sum(ind);
end
oldtmp=newtmp;
end