How to get connected component from adjacency matrix
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
lingfeng zhou
am 13 Aug. 2016
Kommentiert: ali ganjbakhsh
am 9 Dez. 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/155703/image.png)
0 Kommentare
Akzeptierte Antwort
Guillaume
am 14 Aug. 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 Kommentare
Weitere Antworten (1)
Image Analyst
am 13 Aug. 2016
Bearbeitet: Image Analyst
am 13 Aug. 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 Kommentare
Image Analyst
am 14 Aug. 2016
Unaccept my Answer and see if Guillaume's gives you the answer and if it does, accept his answer to give him credit.
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!