how can I wired two edges and replace it in an adjacency matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
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';
disp(B);
Here matrix is
댓글 수: 0
답변 (1개)
Vinay
2024년 8월 7일
편집: Vinay
2024년 8월 7일
Hello mks,
To rewire an adjacency matrix by replacing two edges at random in MATLAB, the "randperm" function can be used for selection.
An adjacency matrix represents a finite graph where matrix elements indicate vertex adjacency. The "randperm(n)" function generates a random permutation of integers from 1 to "n", and "randperm(n,k)" selects "k" unique integers randomly from 1 to "n".
Identify unconnected pairs of nodes in the adjacency matrix and use "randperm" to randomly select two pairs. Then, update the adjacency matrix by using the selected edges to reflect the rewired connections. This process ensures a random yet systematic modification of the graph structure.
For more information about the MATLAB function “randperm,” you can utilize the documentation link provided below: https://www.mathworks.com/help/matlab/ref/randperm.html
% given intial adjacency matrix B we can extract the current edges
[row,col] = find(B ==1)
current_edges = [row,col]
% Get the indices of potential new edges
[row, col] = find(B == 0);
possible_edges = [row, col];
% Exclude self-loops in the edges
possible_edges = possible_edges(row ~= col, :);
% Randomly select two new edges to add
new_edges = possible_edges(randperm(size(possible_edges, 1), 2), :)
% utilize for loop to update the adjaceny matrix
for i = 1:2
B(new_edges(i,1),new_edges(i,2)) = 1;
B(new_edges(i,2),new_edges(i,1)) = 1;
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Construction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!