Home > Blockchain >  Extracting upper off-diagonal elements of square matrix in row order
Extracting upper off-diagonal elements of square matrix in row order

Time:12-08

I can't seem to get this right. I have a matrix A, and it's upper off-diagonal elements in column order

A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
A[col(A) > row(A)]
[1]  5  9 10 13 14 15

How would I get these elements in row order? In this case: 5 9 13 10 14 15

Thank you

CodePudding user response:

m <- matrix(1:25, 5, 5)

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25

t(m)[lower.tri(m)]

[1]  6 11 16 21 12 17 22 18 23 24
  •  Tags:  
  • r
  • Related