필터 지우기
필터 지우기

How to get connected component from adjacency matrix

조회 수: 25 (최근 30일)
lingfeng zhou
lingfeng zhou 2016년 8월 13일
댓글: ali ganjbakhsh 2020년 12월 9일
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.

채택된 답변

Guillaume
Guillaume 2016년 8월 14일
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
lingfeng zhou
lingfeng zhou 2016년 8월 14일
It's great and that's exactly what I want. I have never contact with this function before.Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 8월 13일
편집: Image Analyst 2016년 8월 13일
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
lingfeng zhou
lingfeng zhou 2016년 8월 14일
Thank you anyway!
Image Analyst
Image Analyst 2016년 8월 14일
Unaccept my Answer and see if Guillaume's gives you the answer and if it does, accept his answer to give him credit.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by