查找复杂单元中所有嵌套单元的数量

编程入门 行业动态 更新时间:2024-10-17 05:35:17
本文介绍了查找复杂单元中所有嵌套单元的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个嵌套的单元格,它代表树状结构:

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}};

我想找出Matlab中个节点的数量.我在下面放了一张简单的图片,可以帮助您更准确地了解我要寻找的内容:

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:

谢谢.

推荐答案

如果我理解正确,则需要单元格本身即单元格的数量. 然后,您可以递归地遍历单元格的单元格(和数字),并使用iscell检查以查看哪些元素是单元格.参见以下内容,其中totnod最终给出了节点数.

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

更多推荐

查找复杂单元中所有嵌套单元的数量

本文发布于:2023-11-30 07:18:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   单元   数量   单元中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!