I have a matrix (dimension can be large) and want to retrieve all upper triangular elements then order them by off-diagonal order. For example, given A = matrix(1:25, 5, 5)
, the output res
is
res = c( c(6,12,18,24), c(11,17,23), c(16,22), c(21) )
## i.e, res = c(offdiag_vect_1, offdiag_vect_2, offdiag_vect_3, offdiag_vect_4)
I know the upper triangular elements can be retrieved through A[upper.tri(A, diag=FALSE)]
, but not sure how to re-order them in a c(offdiag_vect_1, offdiag_vect_2, ..., offdiag_vect_(n-1))
manner in an efficient way.