Azzera filtri
Azzera filtri

generate weighted incidence matrix

2 visualizzazioni (ultimi 30 giorni)
Lou
Lou il 20 Ago 2015
Modificato: Lou il 25 Ago 2015
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 :)

Risposte (0)

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by