Effacer les filtres
Effacer les filtres

generate weighted incidence matrix

2 vues (au cours des 30 derniers jours)
Lou
Lou le 20 Août 2015
Modifié(e) : Lou le 25 Août 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 :)

Réponses (0)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by