How to find connected components in Matlab?
array A = 2 3 2 5 4 8 5 6 7 8 I'd like to get the result as 'conidx = [2 3 5 6] and [4 7 8]'. One of the values of [2 3] exists in the 2nd row, One of the values of [2 5] exists in the 4th row, so [2 3], [2 5] and [5 6] are connected, finally I can get the connected indices as [2 3 5 6]. Otherwise, one of the values of [4 8] exists in the 5th row, so [4 8] and [7 8] are connected, finally I can get the connected indices as [4 7 8]. [3]<-->[2]<-->[5]<-->[6] and [4]<-->[8]<-->[7] build a graph and use graphconncomp G = sparse( A(:,1), A(:,2), 1, max(A(:)), max(A(:)) ); G = G + G.'; %' make graph