MATLAB: How to get connected component from adjacency matrix

adjacency matrixgraphImage Processing Toolboxnodes

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.

Best Answer

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');
Related Question