# R program to calculate# trace of a matrix# Loading library
library(psych)# Creating a matrix
A = matrix(
c(6,1,1,4,-2,5,2,8,7),
nrow =3,
ncol =3,
byrow =TRUE)
A
# Calling tr() function
cat("Trace of A:\n")
tr(A)
R
输出
[,1][,2][,3][1,]611[2,]4-25[3,]287Trace of A:[1]11
R
例2 :
# R program to calculate# trace of a matrix# Loading library
library(psych)# Creating a matrix
A = matrix(c(1:9),3,3)
A
# Calling tr() function
cat("Trace of A:\n")
tr(A)