我有以下代码从给定矩阵构造图形。
获得的图形有两种类型的节点:类型O
和类型S
。
B=[0 0 0 0;0 0 1 0;1 1 1 0;0 1 0 0;1 0 1 0;0 1 1 0;0 1 0 1;0 1 0 1;0 0 0 1; 0 0 0 1]; % Example Matrix
disp('The Matrix of the system is the following');
display(B); % Get the system matrix
nNodeCol = size(B,2); % one node for each column of B
nNodeLine = size(B,1)/2; % one node for every two lines of B
% First the column nodes, then the line nodes:
nodeNames = [cellstr(strcat('O',num2str((1:size(B,2))'))) ; cellstr(strcat('S',num2str((1:size(B,1)/2)')))];
% Adjacency matrix adj, adj(i,j)=1 means there is an edge from node#i to node#j:
adj = zeros(nNodeCol+nNodeLine); % square matrix which size is the number of nodes
adj(1:nNodeCol, nNodeCol+1:end) = B(1:2:end,:)'; % edge from a column node to a line node is added for all the 1 in the first line of the node in the matrix
adj(nNodeCol+1:end, 1:nNodeCol) = B(2:2:end,:); % edge from the line node to a column node is added for all the 1 in the second line of the node in the matrix
% Creation of the graph:
G = digraph(adj,nodeNames);
for x=1: nNodeCol
v{x} = dfsearch(G,nodeNames{x}); % Finding paths from nodes type O .
end
celldisp(v);
现在,我们有了数组v
,其中每个单元格都包含一组节点,这些节点是O
类型节点上dfsearch的结果(因此v{1}
包含{{1}的结果继续......),我得到了以下结果:
dfsearch(G,O1)
现在,我的问题如下:我想获得另一个数组v{1}= (O1,S2,O2,S4,S5,S3,O3)
v{2}= (O2,S2,S4,S5)
v{3}= (O3,S2,O2,S4,O4,S5,S3)
v{4}= (O4,S4,O2,S2,S5)
,其大小为W
,即nNodeLine
类型的节点数和每个{{1} }包含S
W{x}
的第一个元素,其中v{x}
是(v{x}{1})
的一部分。例如,Sx
将包含v{x}
的所有第一个元素,其中包含w{1}
。所以我必须得到以下内容:
v{x}
有什么建议吗?
答案 0 :(得分:0)
检查一下,
for i=1:5
ss=['S',int2str(i)];
k=0;
for j=1:4
if not(isempty(find(strcmp(v{1,j,:},ss))))
w{1,i}{k+1,1}=v{1,j}{1,1};
end
k=k+1;
end
if isempty(w{1,i})
w{1,i}={};
end
end
它有小bug,我目前正忙着解决它,试着自己解决它。如果你不能,请在评论中告诉我,我会在有空的时候解决它。