我需要帮助编写执行以下操作的脚本:
data = [1 1 2;
2 1 3;
3 3 4;
3 3 5;
4 3 6]
该脚本需要检查第二列中的数字是否重复。下面的草图解释了我想要完成的任务:
对于第二列中的每个重复值,我希望第三列中共享第二列中每个重复值的所有值作为正确列中的条目,如果该值不重复,则表示 2--->4例如,脚本需要检查数字 2 是否属于上一层,如果来自它的任何数字都属于下一层
Shai提供的脚本解决了上述问题
Although, now comes the step of branch numbering, need to number the branches in layers away from the root(node 1) as shown in the pic below. the numbering of branches in one layer starts only after all the branches in the previous layer have been numbered. However, considering the tress structure as a radial electrical distributed generation system, when doing backward sweep iteration(to calculate the current en each node). how can the script tell if for example line7 between nodes 8 and 5 where 5 is also part of line4 etc...? any experts suggestions are welcome
希望我没听错
data = [1 1 2; 2 1 3; 3 2 4; 4 3 5; 5 3 6; 6 4 7;7 5 8];
mx=max(data(:,3)); %// maximal node index
估计每个节点的层数(节点1
是在层中0
):
ly = zeros(mx,1);
for ii=2:mx,
ly(ii) = ly( data( data(:,3)==ii, 2 ) )+1; %// get the layer of the parent
end
查看每个节点的层索引(ly
):
0 1 1 2 2 2 3 3
现在我们根据层将节点拆分为列
for ci=1:max(ly),
c{ci} = find(ly==ci); %// put all nodes of layer ci in a cell
end;
结果矩阵中的行数等于具有最大节点数的层:
mxc = max(cellfun(@numel,c));
构建结果
res=zeros(mxc,max(ly));
for ci=1:numel(c),
res(1:numel(c{ci}),ci)=c{ci}(:);
end;
结果是:
res =
2 4 7
3 5 8
0 6 0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)