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 Center および File ExchangeBiotech and Pharmaceutical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by