Home > Software engineering >  Given adjacency matrix and matrix containing edge list, how do i correctly encoded 1's into adj
Given adjacency matrix and matrix containing edge list, how do i correctly encoded 1's into adj

Time:10-27

A =

   4 5 8 10
4  0 0 0  0
5  0 0 0  0
8  0 0 0  0
10 0 0 0  0

with 4, 5, 8, 10 be the column and row names of A

edge =
[,1] [,2]
[1,]    4   10
[2,]    5    8
[3,]    5   10

after encoding the ones, A should look like

A = 
   4 5 8 10
4  1 0 0  1
5  0 1 1  1
8  0 1 1  0
10 1 1 0  1

I tried A[edge] = 1, but got out of subscript.

I tried A[edge] = 1, but got out of subscript.

CodePudding user response:

Just convert your numeric matrix to character:

A[array(as.character(edge), dim(edge))] <- 1
A <- A   t(A)
diag(A) <- 1
A

   4 5 8 10
4  1 0 0  1
5  0 1 1  1
8  0 1 1  0
10 1 1 0  1
  • Related