How can I connect 25 nodes with 50 connections?

1 次查看(过去 30 天)
I have 25 nodes that need 50 (random) connections. Below my code, any suggestions?
figure;
load('usborder.mat','x','y','xx','yy');
rng(6,'twister')
nStops = 25;
stopsLon = zeros(nStops,1);
stopsLat = stopsLon;
n = 1;
while (n <= nStops)
xp = rand*1.5;
yp = rand;
if inpolygon(xp,yp,x,y)
stopsLon(n) = xp;
stopsLat(n) = yp;
n = n+1;
end
end
plot(x,y,'k--');
hold on
plot(stopsLon,stopsLat,'xr')
hold off

采纳的回答

Elizabeth Reese
Elizabeth Reese 2017-8-10
There are a couple of ways to do this. One way is to use randi to generate two integers between 1 and 25. They will act as indexes into your list of nodes. You can then use these numbers to index into your stopsLon and stopsLat and connect the nodes at those two indexes. If you are concerned about self-loops or having only unique paths, then you can create a 25x25 connectivity matrix or similar structure to keep track of which connections you have already made.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by