简体   繁体   中英

Extracting off-diagonal elements of a matrix in R

The off-diagonal elements of the mat_rix below are all 0 s.

To extract the off-diagonal elements, I use a solution that I found on SO: odiag <- function(x) x[(n <- nrow(x))^2-(1:n)*(n-1)]

But when I use odiag(mat_rix) , the output contains non-0 elements. I wonder what's the problem and how to fix it?

x="
 0.4850377 0.000000 0.00000000 0.00000000 0.0000000 0.000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.510766 0.00000000 0.00000000 0.0000000 0.000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.05767389 0.00000000 0.0000000 0.000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.07539841 0.0000000 0.000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.3134951 0.000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.0000000 0.679101 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.0000000 0.000000 0.5067036 0.0000000 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.0000000 0.000000 0.0000000 0.1829717 0.0000000 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.0000000 0.000000 0.0000000 0.0000000 0.3722585 0.0000000 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.0000000 0.000000 0.0000000 0.0000000 0.0000000 0.3907239 0.0000000
 0.0000000 0.000000 0.00000000 0.00000000 0.0000000 0.000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0287955"

mat_rix <- as.matrix(read.table(text=x))

odiag(mat_rix)

[1] 0.000000 0.000000 0.000000 0.000000 0.000000 0.679101 0.000000 0.000000 0.000000 0.000000 0.000000

If we need the off-diagonal, create a function where the row index is not equal to col umn index

odiag <- function(x) x[col(x) != row(x)]
odiag(mat_rix)

If we need the values that are one up/one down from the diagonal

odiag2 <- function(x, offdiag = "up") {
    ind <- if(offdiag == "up") -1 else 1
    x[row(x) - col(x) == ind]
}
odiag2(mat_rix, "up")
[1] 0 0 0 0 0 0 0 0 0 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM