How can I connect 25 nodes with 50 connections?

1 vue (au cours des 30 derniers jours)
Linda Grafen
Linda Grafen le 4 Août 2017
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

Réponse acceptée

Elizabeth Reese
Elizabeth Reese le 10 Août 2017
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.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by