0

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

Matthew
  • 3
  • 1
  • 4
  • 1
    I get `clusters = {[1 2 3], 4, 5, [6 8 9], 7}` (tested in R2017b and in R2018b). Can you check? – Luis Mendo Feb 13 '19 at 12:12
  • @LuisMendo thanks for your prompt response. Nodes 4, 5 and 7 are not meant to be part of the results. I have added the .mat file of the cluster node I used which is not giving me the expected result. – Matthew Feb 14 '19 at 05:16
  • @LuisMendo mine is still returning clusters: [1,2,3];[4,5,6]. How do I force it to return the exact node numbers on the graph shown? Thanks. – Matthew Feb 15 '19 at 04:19
  • I got an error trying to load the file. Anyway, you should post a mininal code example that reproduces the problem, rather than link a .mat file – Luis Mendo Feb 15 '19 at 10:04
  • @LuisMendo I have added the minimal code that reproduces the problem. You can try it yourself. – Matthew Feb 16 '19 at 04:09
  • After your fode, try something like `nodes = table2array(G.Nodes); result = cellfun(@(x) nodes(x), clusters, 'uniformoutput', false)` – Luis Mendo Feb 18 '19 at 09:56
  • @LuisMendo. Thank you once again. The code executed and I was able to access the contents of the clusters with "result{:}" which returned the exact node numbers. – Matthew Feb 19 '19 at 07:15
  • Great! I posted it as an answer so that you can accept if you want – Luis Mendo Feb 20 '19 at 09:27

1 Answers1

0

After your code, try something like

nodes = table2array(G.Nodes);
result = cellfun(@(x) nodes(x), clusters, 'uniformoutput', false);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147