# Sorting Equal Elements of Matrix into Groups in Cell Array

3 views (last 30 days)
Chad on 10 Jun 2020
Commented: Chad on 10 Jun 2020
Lets say C is an nx2 matrix where n can range from 1 to 15. All elements of C can range from 1-6. I want to form groups in the form of a cell array of like pairs of numbers. C1 will be a cell array which describes the ways the numbers of C1 are connected. The easiest way to describe this is just by examples:
C = [1 2; 1 3; 2 3] then C1 = { [ 1 2 3] }
C = [3 4; 3 5; 3 6; 4 5; 4 6; 5 6] then C1 = { [ 3 4 5 6] }
C = [1 2; 1 6; 2 6; 3 4; 3 5; 4 5] then, C1 = { [1 2 6], [3 4 5] }.
C = [1 3; 1 4; 3 4; 5 6] then C1 = { [1 3 4], [5 6]}
C = [1 2; 3 6; 4 5] then C1 = { [1 2], [3 6], [4 5] }
Hopefully you can see the trend. Any help is appreciated.

Fabio Freschi on 10 Jun 2020
Edited: Fabio Freschi on 10 Jun 2020
I had the same problem to detect isolated nodes in a graph: conncomp is the matlab command that gets the job done:
% create the graph object
G = graph(C(:,1),C(:,2));
% get disconnected parts
bnd = conncomp(G,'OutputForm','cell');
##### 1 CommentShowHide None
Chad on 10 Jun 2020
Thank you, very easy and simple solution.