Create a n*n grid undirected graph

1 次查看(过去 30 天)
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?

回答(1 个)

KSSV
KSSV 2021-11-4
[x,y] = meshgrid(1:10) ;
dt = delaunayTriangulation(x(:),y(:)) ;
triplot(dt)
  2 个评论
ANDREA DI MARTINO
ANDREA DI MARTINO 2021-11-4
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 2021-11-9
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Directed Graphs 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by