Similarly to upper.tri and lower.tri, returns a matrix of logicals to identify the diagonal of a square matrix

diagonal(M, output = c("matrix.logical", "vector.values"))

Arguments

M

matrix or other R object with length(dim(x)) == 2. For back compatibility reasons, when the above is not fulfilled, as.matrix(x) is called first

output

choose to either return a matrix of logical values (TRUEs on the diagonal, i.e. comparable behavior than diag for instance) or a vector of the subsetted values of the matrix

Value

square logical matrix with diagonal of TRUEs

Examples

M<- matrix(sample(1:10,16,replace = TRUE),4,4) diagonal(M)
#> [,1] [,2] [,3] [,4] #> [1,] TRUE FALSE FALSE FALSE #> [2,] FALSE TRUE FALSE FALSE #> [3,] FALSE FALSE TRUE FALSE #> [4,] FALSE FALSE FALSE TRUE