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