Simon Harmel
Simon Harmel

Reputation: 1489

Extracting off-diagonal elements of a matrix in R

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

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

Upvotes: 3

Views: 1567

Answers (1)

akrun
akrun

Reputation: 887148

If we need the off-diagonal, create a function where the row index is not equal to column 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

Upvotes: 4

Related Questions