Effacer les filtres
Effacer les filtres

How to get connected component from adjacency matrix

22 vues (au cours des 30 derniers jours)
lingfeng zhou
lingfeng zhou le 13 Août 2016
Commenté : ali ganjbakhsh le 9 Déc 2020
The adjacency matrix is already known.
what I want to do is showed in the picture below.
I don't care about the order of the vertex.

Réponse acceptée

Guillaume
Guillaume le 14 Août 2016
A lot of graph functions have been added in R2015b. In particular, you have conncomp which gives you exactly what you want:
adjacencyMatrix =[0 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0
1 1 0 0 1 0 1 0 1 0
0 0 0 0 0 0 0 1 0 0
1 1 1 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 1
1 1 1 0 1 0 0 0 1 0
0 0 0 1 0 0 0 0 0 1
1 1 1 0 1 0 1 0 0 0
0 0 0 0 0 1 0 1 0 0]
G = graph(adjacencyMatrix);
plot(G); %view the graph
bins = conncomp(G);
binnodes = accumarray(bins', 1:numel(bins), [], @(v) {sort(v')});
fprintf('number of Regions = %d\n\n', numel(binnodes));
for binidx = 1:numel(binnodes)
fprintf('All these nodes are connected:%s\n', sprintf(' %d', binnodes{binidx}));
end
fprintf('\n');
  2 commentaires
lingfeng zhou
lingfeng zhou le 14 Août 2016
It's great and that's exactly what I want. I have never contact with this function before.Thank you so much.
ali ganjbakhsh
ali ganjbakhsh le 9 Déc 2020
so tnx

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 13 Août 2016
Modifié(e) : Image Analyst le 13 Août 2016
Use bwlabel() and regionprops:
[labeledMatrix, numberOfRegions] = bwlabel(A);
props = regionprops(labeledMatrix, 'PixelList');
labeledMatrix gives an ID number to each connected region. props has all the information on all the elements in each connected region. You can get indexes (rows and columns), values, areas, etc. depending on what you ask regionprops() for.
  7 commentaires
lingfeng zhou
lingfeng zhou le 14 Août 2016
Thank you anyway!
Image Analyst
Image Analyst le 14 Août 2016
Unaccept my Answer and see if Guillaume's gives you the answer and if it does, accept his answer to give him credit.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by