Assuming you want that identical nodes are nodes with the same X and Y coordinates, then first I'd add these coordinates to the Nodes tables of each graph:
s = [1 1 2 3 4 4 6 7 8];
t = [2 5 3 5 6 7 9 9 9];
x = [1 1 1 2 2 2 3 3.5 3];
y = [1 2 3 4 1 3 4 3.5 3];
G1 = graph(s,t);
s2 = [1 2 3 4 1];
t2 = [2 3 4 1 5];
x2 = [1.5 1.5 3.5 3.5 1];
y2 = [1.5 3.5 3.5 1.5 1];
G2 = graph(s2,t2);
G1.Nodes.X = x(:); G1.Nodes.Y = y(:);
G2.Nodes.X = x2(:); G2.Nodes.Y = y2(:);
You can then compute the unions based on these X and Y:
mergedXY = union(G1.Nodes{:, {'X', 'Y'}}, G2.Nodes{:, {'X', 'Y'}}, 'rows');
mergedNodes = array2table(mergedXY, 'VariableNames', {'X', 'Y'});
[~, remapG1] = ismember(G1.Nodes{:, {'X', 'Y'}}, mergedXY, 'rows');
[~, remapG2] = ismember(G2.Nodes{:, {'X', 'Y'}}, mergedXY, 'rows');
mergedEdges = table([remapG1(G1.Edges.EndNodes); remapG2(G2.Edges.EndNodes)], 'VariableNames', {'EndNodes'});
G = simplify(graph(mergedEdges, mergedNodes));
figure;
plot(G, 'XData', G.Nodes.X, 'YData', G.Nodes.Y);
Best Answer