필터 지우기
필터 지우기

generate weighted incidence matrix

조회 수: 2 (최근 30일)
Lou
Lou 2015년 8월 20일
편집: Lou 2015년 8월 25일
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개)

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by