Learn R Programming

tensr (version 1.0.1)

tr: Trace of a matrix.

Description

Returns the sum of the diagonal elements of a matrix.

Usage

tr(X)

Arguments

X

A matrix whose diagonal elements will be added together.

Value

The sum of the diagonal elements of X.

Details

This returns the trace of a matrix, which is just the sum of its diagonal elements.

Examples

Run this code
# NOT RUN {
X <- matrix(1:4, nrow = 2, ncol = 2)
X
tr(X)
# }

Run the code above in your browser using DataLab