generate weighted incidence matrix
2 views (last 30 days)
Show older comments
Hello, how can I generate a random weighted incidence matrix? Something like this:
3 4 0 0 0 0 0 0
-3 0 1 1 7 0 0 0
0 0 -1 0 0 3 0 0
0 -4 0 -1 0 0 5 0
0 0 0 0 -7 0 -5 6
0 0 0 0 0 -3 0 -6
And how can I specify them,for example if the number of nodes, the density or the maximum weight is given?
My current solution looks like this:
function [Inc] = randin(n, den, w)
%generated random weighted incidence matrix for a directed graph with
%n nodes, arc denisty den and maximal weight w
m=round(n*n*den); %calculate number of arcs
Inc=zeros(n, m);
for i=1:m
c=randperm(w,1); %calculate random weight
d=randperm(n,1); %determines incoming/outgoing nodes
e=randperm(n,1);
while e==d %incoming/outgoing nodes can not be the same
e=randperm(n,1);
endwhile
Inc(d,i)=c;
Inc(e,i)=-c;
endfor
Inc=sparse(Inc);
endfunction
But I guess this is not a very efficient way to solve the problem, is it?
Thank you :)
0 Comments
Answers (0)
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!