Sorting Equal Elements of Matrix into Groups in Cell Array

1 vue (au cours des 30 derniers jours)
Chad
Chad le 10 Juin 2020
Commenté : Chad le 10 Juin 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.

Réponse acceptée

Fabio Freschi
Fabio Freschi le 10 Juin 2020
Modifié(e) : Fabio Freschi le 10 Juin 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 commentaire
Chad
Chad le 10 Juin 2020
Thank you, very easy and simple solution.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by