我在MATLAB中编写了一段代码,可以生成一个有
然而,这段代码效率非常低,特别是当我需要处理像
n
个顶点的随机图,每个顶点有c
个固定的邻居,没有自环(请注意,边是有向的,因此“a连接到b”并不意味着“b连接到a”)。然而,这段代码效率非常低,特别是当我需要处理像
n = 10000
和c = 1000
这样的数量级时。我想知道是否有人能够大幅度优化它,或者提出任何有建设性的建议?function [M]=matsrand(n,c)
MM=0; %arbitrary starting value
while MM ~=n*c
M = sparse(zeros(n));
ctin = zeros(1,n);
for i=1:n
rp = randperm(n); %generate vector of the randomly permuted order of n vertices
rp(rp==i)=[]; %get rid of itself to avoid self connection
noconnect=find(ctin(:)>=c); %generate list that i is not allowed to connect to
where=ismember(rp,noconnect); %returns 1 to the subset noconnect in rp
noconnectind=find(where);
rp(noconnectind(:))=[]; %remove the neurons i is not allowed to connect to
if length(rp)<c
break
else
r=rp(1:c);
end
M(i,r)=1;
ctin(r)=ctin(r)+1;
end
MM=sum(ctin);
end
length(rp)<c
吗? - Gunther Struyf