Hardly worth coding, if it didn't appear in so many formulae in psychometrics, the trace of a (square) matrix is just the sum of the diagonal elements.

tr(m)

Arguments

m

A square matrix

Details

The tr function is used in various matrix operations and is the sum of the diagonal elements of a matrix.

Value

The sum of the diagonal elements of a square matrix. i.e. tr(m) <- sum(diag(m)).

Examples

m <- matrix(1:16,ncol=4) m
#> [,1] [,2] [,3] [,4] #> [1,] 1 5 9 13 #> [2,] 2 6 10 14 #> [3,] 3 7 11 15 #> [4,] 4 8 12 16
tr(m)
#> [1] 34