How get adjacency matrix from this code

17 ビュー (過去 30 日間)
Konstantinos koutsikakis
Konstantinos koutsikakis 2020 年 10 月 30 日
コメント済み: Walter Roberson 2020 年 11 月 1 日
N = input('no. of Nodes');
p = input('Give the possibility');
m = (n*(n-1))/2;
degree=zeros(N,1);
position=zeros(N,2);
for m=1:N
position(m,1)=cos(m/N*2*pi);
position(m,2)=sin(m/N*2*pi);
end
hold on;
plot(position(:,1),position(:,2),'d')
for m=1:N
for n=m+1:N
if(rand(1,1)<p)
degree(m,1)=degree(m,1)+1;
degree(n,1)=degree(n,1)+1;
plot(position([m,n],1),position([m,n],2))
end
end
end
hold off;

回答 (1 件)

Stephan
Stephan 2020 年 10 月 31 日
編集済み: Stephan 2020 年 10 月 31 日
I would recommend to use the inbuilt functions for working with graphs - it makes life much easier:
N = input('no. of Nodes');
p = input('Give the possibility');
% Make adjacency matrix
idx = find(triu(ones(N),1));
V = rand(((N-1)*N)/2,1)<p;
A = zeros(N);
A(idx) = V;
A = A + triu(A,1)';
% create and plot undirected graph object
g = graph(A)
plot(g)
A good start for reading is here:
  4 件のコメント
Konstantinos koutsikakis
Konstantinos koutsikakis 2020 年 11 月 1 日
ok. how to print points in the form of a circle and a adjacency matrix
Walter Roberson
Walter Roberson 2020 年 11 月 1 日
https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.graphplot.layout.html#d122e485652

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by