tr()R语言中的函数用于计算矩阵的迹。矩阵的迹是矩阵主对角线(左上至右下)上的值的总和。
用法: tr(x)
参数:
x:矩阵
范例1:
# 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)输出:
     [, 1] [, 2] [, 3]
[1, ]    6    1    1
[2, ]    4   -2    5
[3, ]    2    8    7
Trace of A:
[1] 11
范例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)输出:
     [, 1] [, 2] [, 3]
[1, ]    1    4    7
[2, ]    2    5    8
[3, ]    3    6    9
Trace of A:
[1] 15
相关用法
- R语言 colSums()用法及代码示例
- R语言 colMeans()用法及代码示例
- R语言 is.matrix()用法及代码示例
- R语言 data.matrix()用法及代码示例
- R语言 as.matrix()用法及代码示例
- R语言 acos()用法及代码示例
- R语言 cos()用法及代码示例
- R语言 cosh()用法及代码示例
- R语言 sin()用法及代码示例
- R语言 sinh()用法及代码示例
- R语言 tanh()用法及代码示例
- R语言 tan()用法及代码示例
- R语言 asin()用法及代码示例
- R语言 acos()用法及代码示例
- R语言 atan()用法及代码示例
- R语言 exp()用法及代码示例
- R语言 factorial()用法及代码示例
- R语言 choose()用法及代码示例
- R语言 cumprod()用法及代码示例
- R语言 cumsum()用法及代码示例
注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Calculate Trace of a Matrix in R Programming – tr() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
