Hi Everyone,
I'd like to create random adjacency matrices for networks in the following form (see image), as you can see I have 6 nodes (n = 6) and each node must have 2 connections (degree = 2) to two other nodes chosen randomly. So the sum of each row must equal 2(or whatever value "degree" is assigned). The matrix must be symmetric with zeros along the diagonal (properties of the adjacency matrix). I have written a code that generates random adjacency matrices but Imnot sure how to constraint the program such that each node has exaclty 2 random connections.
A = zeros(n,n); % Create initial matrix
for i = 1:n
for j = 1:n
if i==j
A(i,j)=0; % Zero values in the diagonal entries
else
A(i,j)=round(rand); % Choosing random 0 and 1 values
A(j,i)=A(i,j); % Make A matrix symmetric
end
end
end
Any help is greatly appreciated, Thank you!

 채택된 답변

David Hill
David Hill 2021년 5월 13일

0 개 추천

Graph has to be a polygon.
numNodes=6;
a=randperm(numNodes);
b=circshift(a,1);
g=graph(a,b);
plot(g);
m=full(adjacency(g));

댓글 수: 3

NeGM
NeGM 2021년 5월 13일
Hi David,
this is perfect given that each node has 2 connections but I'd like to know how to contraint it si that I can change the number of nodes and edges for instance how can I generate an adjacency matrix with n = 20 and degrees = 3 (number of connections) to 3 different nodes. any suggestions? Thank you so much in advance
David Hill
David Hill 2021년 5월 13일
편집: David Hill 2021년 5월 13일
Some combinations of nodes and degrees will not work.
while 1
numNodes=20;
degrees=3;
n=repelem(1:numNodes,degrees);
s=[];t=[];
flag=1;
while ~isempty(n)
N=unique(n);
a=sum(n==N(1));
b=nchoosek(N(2:end),a);
try
c=b(randi(length(b)),:);
catch
flag=0;
break;
end
s=[s,n(n==N(1))];
t=[t,c];
n=n(a+1:end);
for k=1:a
try
n(find(n==c(k),1))=[];
catch
flag=0;
break;
end
end
end
if flag
g=graph(s,t);
plot(g);
m=full(adjacency(g));
break;
end
end
NeGM
NeGM 2021년 5월 13일
Hey David, Thank you it works pretty well!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2021년 5월 13일
편집: Matt J 2021년 5월 13일

0 개 추천

Perhaps as follows,
n=6;
T=nan(n,2);
map=isnan(T);
while any(map(:))
[i,~]=find(map,1);
if ~map(i,1)
exclude=[i,T(i,1)];
elseif ~map(i,2)
exclude=[i,T(i,2)];
else
exclude=i;
end
pool=setdiff(find(any(map,2)),exclude);
if isempty(pool)
T=nan(n,2); map=isnan(T); continue
end
p=numel(pool);
j=pool(randi(p));
if map(i,1),
T(i,1)=j;
map(i,1)=0;
else,
T(i,2)=j;
map(i,2)=0;
end
if map(j,1),
T(j,1)=i;
map(j,1)=0;
else,
T(j,2)=i;
map(j,2)=0;
end
end
s=[(1:n), (1:n)].';
t=[T(:,1); T(:,2)];
st=unique(sort([s,t],2),'rows');
G=graph(st(:,1),st(:,2));
degree(G)
ans = 6×1
2 2 2 2 2 2

댓글 수: 3

NeGM
NeGM 2021년 5월 13일
hi Matt,
Under your code each node has a degree of 4, is there a way to contraint the connection to only have a specified number? Thank you
Matt J
Matt J 2021년 5월 13일
Fixed it.
NeGM
NeGM 2021년 5월 13일
Thank you

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

질문:

2021년 5월 13일

편집:

2021년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by