How can I expedite the speed of this code?

1 visualización (últimos 30 días)
Rayan Glus
Rayan Glus el 7 de Oct. de 2021
Editada: Rayan Glus el 8 de Oct. de 2021
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;

Respuestas (0)

Categorías

Más información sobre Biotech and Pharmaceutical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by