M=[1 2;2 5;3 4;4 6;6 7;6 8;6 9;6 10];
G=graph(M(:,1),M(:,2))
[bins,binsizes]=conncomp(G);
When I use the built in function conncomp, MATLAB always assumes that first component of graph G is the one which node 1 belongs to.
The output bins in the given example is:
bins=[1 1 2 2 1 2 2 2 2 2] How can I make the first component of my graph is where node 6 (for example) belongs to?
Your help would be appreciated.
Thanks!

 Akzeptierte Antwort

Matt J
Matt J am 14 Dez. 2020
Bearbeitet: Matt J am 14 Dez. 2020

1 Stimme

M=[1 2;2 5;3 4;4 6;6 7;6 8;6 9;6 10];
G=graph(M(:,1),M(:,2));
[bins,binsizes]=conncomp(G);
idx=[2,1];
binsizes=binsizes(idx);
bins=idx(bins)
bins = 1×10
2 2 1 1 2 1 1 1 1 1

3 Kommentare

Asaf McRock
Asaf McRock am 15 Dez. 2020
Thank you very much, Matt!
This is so clever. But I wonder if it works for higher number of components.
Could your trick be generalized? I'm new to programming, so excuse my ignorance.
You would just need to change idx to an appropriate permutation of 1:N, e.g.,
idx=[3,1,2,4]
Asaf McRock
Asaf McRock am 15 Dez. 2020
Awesome, got it.
Thank you again.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by