How can I expedite the speed of this code?

조회 수: 1 (최근 30일)
Rayan Glus
Rayan Glus 2021년 10월 7일
편집: Rayan Glus 2021년 10월 8일
Hello,
I found a nice code for generating Scale Free networks (Barabasi Algorithm) on File Exchange but it's slow specially when I try to generate more than 10000 nodes. I would appreciate if someone can tell me what is the most expensive piece of line here and how can I expedite the simulation run speed. Also, I wonder if there is a built in function in MATLAB for generating Scale Free Networks.
Thanks all.
function SFNet = SFNG(Nodes, mlinks, seed)
seed = full(seed);
pos = length(seed);
%if (Nodes < pos) || (mlinks > pos) || (pos < 1) || (size(size(seed)) ~= 2) || (mlinks < 1) || (seed ~= seed') || (sum(diag(seed)) ~= 0)
% error('invalid parameter value(s)');
%end
%if mlinks > 5 || Nodes > 15000 || pos > 15000
% warning('Abnormally large value(s) may cause long processing time');
%end
rand('state',sum(100*clock));
Net = zeros(Nodes, Nodes, 'single');
Net(1:pos,1:pos) = seed;
sumlinks = sum(sum(Net));
while pos < Nodes
pos = pos + 1;
linkage = 0;
while linkage ~= mlinks
rnode = ceil(rand * pos);
deg = sum(Net(:,rnode)) * 2;
rlink = rand * 1;
if rlink < deg / sumlinks && Net(pos,rnode) ~= 1 && Net(rnode,pos) ~= 1
Net(pos,rnode) = 1;
Net(rnode,pos) = 1;
linkage = linkage + 1;
sumlinks = sumlinks + 2;
end
end
end
clear Nodes deg linkage pos rlink rnode sumlinks mlinks
SFNet = Net;

답변 (0개)

카테고리

Help CenterFile Exchange에서 Biotech and Pharmaceutical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by