Extract opposite-diagonal (not off-diagonal) elements of a matrix
问题 May be this is too simple of a question but I couldn't find a functional answer. How can we extract the opposite diagonal elements of any square matrix in R? In the example below that would be: 7, 2, 8 . r <- matrix(c(1, 5, 8, 1:3, 7:9), 3) 回答1: An approach could be r[(n<-nrow(r))^2-(1:n)*(n-1)] # [1] 7 2 8 ## microbenchmark (matrix(1:1e6,1000)) # Unit: microseconds # expr min lq mean median uq max neval # r[(n<-nr... 26.897 39.0075 65.36835 47.309 85.9345 316.97 100 # diag(r[,... 18070.388