matlabnodesgraph-theoryverticessplit-apply-combine

How to use splitapply/findgroups on clustered/non-sequential graphs?


I need to implement splitapply function to non-sequential node index in graph.

I implemented the splitapply function on a graph that has non-sequential clusters. The index numbers of the returned clusters were sequentially numbered but the graph vertices are not sequentially numbered. I want it to return the exact node indices from the original graph.

S={' 1',' 1',' 2',' 6',' 6',' 8'};
T={' 2',' 3',' 3',' 8',' 9',' 9'};
weight=[2; 2; 2; 2; 2; 2];
G=graph(S,T,weight);

plot(G)
bins=conncomp(G);
clusters = splitapply(@(x) {x}, 1:numnodes(G), bins);

The actual results: clusters: [1,2,3];[4,5,6]
The expected results: clusters: [1,2,3];[6,8,9]

sample graph


Solution

  • After your code, try something like

    nodes = table2array(G.Nodes);
    result = cellfun(@(x) nodes(x), clusters, 'uniformoutput', false);