Nodes belongto which Graph?

Dear matlab,
Please let me know if we can findout from the graph table which nodes belong to which graph, as in G.Edges in the graph function only shows connected nodes and linkages and weights, i.e node1 node2 weight
Can we have information that shows node and graph it belongs to, ex node1 graph1 node2 graph1 node3 graph2 node4 graph2 node5 graph2 node6 graph3

2 Comments

Did you perhaps want to ask about which vertices belong to which subgraph?
Hi An adjacency matrix (table) is pure a node to node connection. Either, movement of: TOP to RIGHT to TOP to RIGHT ..... or others are generated graphical model. whatever you want, it scientifically possible to get

Sign in to comment.

Answers (1)

If you have a series of graphs, G1, G2, G3, ... each of which has been created with graph(), or digraph() then:
Graphs = {G1, G2, G3, ... }; %create a cell array of all of the graphs
GraphNames = {'graph1', 'graph2', 'G3', ...}; %names of graphs to use in report
numgraph = length(Graphs);
nodes_are_numeric = false(numgraph, 1);
for K = 1 : numgraph
these_nodes = unique(Graphs{K}.Edges.EndNodes(:));
if ~iscell(these_nodes); these_nodes = num2cell(these_nodes); end
used_nodes{K} = these_nodes;
nodes_are_numeric(K) = isnumeric(these_nodes{1});
end
node_fmt_is_char = ~all(nodes_are_numeric);
if all(nodes_are_numeric) ~= any(nodes_are_numeric)
%there is a mix of node types, need to convert the numeric ones to character
for K = find(nodes_are_numeric(:)).'
used_nodes{K} = cellfun(@num2str, used_nodes{K}, 'Uniform', 0);
end
end
unique_nodes = unique( vertcat(used_nodes{:}) );
num_unique_nodes = length(unique_nodes);
node_belongs_to = cell(num_unique_nodes, 1);
for K = 1 : num_unique_nodes
this_node = unique_nodes{K};
node_is_in_graph = cellfun(@(N) ismember(this_node, N), used_nodes);
node_member_of = GraphNames(node_is_in_graph);
node_belongs_to{K} = [{this_node}, node_member_of];
end
%now the printed report
if node_fmt_is_char
fmt = 'Node %s belongs to graphs: ';
else
fmt = 'Node %g belongs to graphs: ';
end
for K = 1 : num_unique_nodes
this_entry = node_belongs_to{K};
fprintf(fmt, this_entry{1});
fprintf('%s, ', this_entry{2:end} );
fprintf('\n');
end

1 Comment

Dear Walter,
I am facing below error during execution
Error using cell/unique (line 85) Input A must be a cell array of strings.
Error in Randomised_Spectral_clustering_gplot (line 163) unique_nodes = unique( vertcat(used_nodes{:}) );
It gets resolved with unique_nodes = unique( cell2table(vertcat(used_nodes{:})) );

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Asked:

on 22 Oct 2016

Commented:

on 24 Oct 2016

Community Treasure Hunt

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

Start Hunting!