How can I expedite the speed of this code?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Biotech and Pharmaceutical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!