Create a n*n grid undirected graph

4 Ansichten (letzte 30 Tage)
ANDREA DI MARTINO
ANDREA DI MARTINO am 4 Nov. 2021
Hi all, I can't figure out how I can solve this problem. I'm working with undirected graph in matlab. I created graph with 15 nodes with this code:
s = [1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 6 8 10 2 11 7 8 10 13 10 11 13 15];
t = [2 6 7 3 6 7 8 4 7 8 9 5 8 9 10 13 9 10 11 14 13 11 14 12 9 14 13 15 15 15 14 12];
G = graph(s,t);
I now need to create a graph with 100 nodes in a grid shape. Something like the picture below:
Whit this code:
grid=reshape(1:100,10,10)';
grid =
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
I'm able to create a grid of number but how can I trasform this into a graph like the previous pictures?

Antworten (1)

KSSV
KSSV am 4 Nov. 2021
[x,y] = meshgrid(1:10) ;
dt = delaunayTriangulation(x(:),y(:)) ;
triplot(dt)
  2 Kommentare
ANDREA DI MARTINO
ANDREA DI MARTINO am 4 Nov. 2021
I solved using this add on: https://blogs.mathworks.com/steve/2015/12/14/image-based-graphs/ . I create a graph in this way:
G = imageGraph([10 10],8);
G.Nodes = removevars(G.Nodes,{'PixelIndex'});
plot(G);
and I obtain this:
I'm going to add wheights and other usefull infos.
ANDREA DI MARTINO
ANDREA DI MARTINO am 9 Nov. 2021
Yeah. I don't write here the construction of the weights to avoid boring stuff, but in the end i simply do this:
n = length(G.Nodes.x);
for i = 1:n
G.Edges.Weight(i) = weights(i);
end
p = plot(G,'MarkerSize',10,'LineWidth',2,'EdgeLabel',G.Edges.Weight,'EdgeFontSize',5);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by