looping through an adjacency matrix

Screenshot 2022-06-28 at 14.44.55

Pls , how do I loop through just the upper or lower triangle of the adjacent matrix?
I am trying to avoid the zeros along the diagonal.

Thanks

You can grab the upper triangle with the upper.tri function. Does that fit your needs?

#Make a matrix
MAT <- matrix(1:25,nrow = 5)
diag(MAT) <- 0
MAT
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    6   11   16   21
[2,]    2    0   12   17   22
[3,]    3    8    0   18   23
[4,]    4    9   14    0   24
[5,]    5   10   15   20    0

#Pull the upper triangle
MAT[upper.tri(MAT)]
 [1]  6 11 12 16 17 18 21 22 23 24
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.