Adjacency matrix.I want to simply the code .

1 回表示 (過去 30 日間)
mks
mks 2023 年 8 月 13 日
コメント済み: mks 2023 年 8 月 14 日
clc;
clear all;
n = 10;
C= zeros(n, n);
C(1,n)=1;
C(1,n-1)=1;
C(2,n)=1;
A = zeros(n, n);
for i = 1:n
for j = 1:n
if j==i+1 || j == i + 2
A(i,j) = 1;
else
A(i, j) = 0;
end
end
end
B1=A+C
B=B1+B1';
G=graph(B);
plot(G)

採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 13 日
移動済み: Bruno Luong 2023 年 8 月 13 日
n = 10;
x=accumarray([2 3 n-1 n]',1);
B = toeplitz(x,x);
G=graph(B);
plot(G)
  2 件のコメント
mks
mks 2023 年 8 月 13 日
How can I write a code when few node are connected five edges and some nodes are connected four,three and 2 edges .please help me on this .
mks
mks 2023 年 8 月 14 日
how can i rewired by replacing 2 edges at random then the code will be ?

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 8 月 13 日
You can get a fair bit of the way with
B1 = diag(ones(n-1,1),1) + diag(ones(n-2,1),2);
You will still need to do the upper right corner, but that is not difficult (use two more diag calls)

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by