MATLAB: How to connect 25 nodes with 50 connections

connectionsMATLABMATLAB and Simulink Student Suitenodes

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

Best Answer

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.
Related Question