I have this graph
Ro=[0 0 0 1 0 0;
0 0 1 0 1 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0]
[row,col]=find(Ro==1)
G = graph(row,col);
plot(G)
I want to write a code that gives me set of separate graph. result shoud be [1,4],[2,3,5]

1 件のコメント

Gareth
Gareth 2019 年 1 月 4 日
Hi Naime,
There probably is an easier way to do this, however I am on R2018b, and on MAC and this is what I quickly came up with:
Ro=[0 0 0 1 0 0;
0 0 1 0 1 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0];
[row,col]=find(Ro==1);
G = graph(row,col);
plot(G)
%%% my simple answer
a = G.conncomp;
myN = 1:size(G.Nodes,1);
for i =1:size(unique(a),2)
myAns{i} = myN(a==i);
end
myAns{:}

サインインしてコメントする。

 採用された回答

Gareth
Gareth 2019 年 1 月 4 日

0 投票

Ro=[0 0 0 1 0 0;
0 0 1 0 1 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0];
[row,col]=find(Ro==1);
G = graph(row,col);
plot(G)
%%% my simple answer
a = G.conncomp;
myN = 1:size(G.Nodes,1);
for i =1:size(unique(a),2)
myAns{i} = myN(a==i);
end
myAns{:}

3 件のコメント

Steven Lord
Steven Lord 2019 年 1 月 4 日
Instead of post-processing the output of a call to conncomp, just ask it to return the output as a cell array directly.
A = conncomp(G, 'OutputForm', 'cell')
The contents of the first cell in A is [1 4], while the second is [2 3 5].
Gareth
Gareth 2019 年 1 月 4 日
Nice:) I did not know it was possible:)
NA
NA 2019 年 1 月 5 日
Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraph and Network Algorithms についてさらに検索

質問済み:

NA
2019 年 1 月 4 日

コメント済み:

NA
2019 年 1 月 5 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by