R语言 计算矩阵的交叉乘积 – crossprod()函数
R语言中的 crossprod() 函数用于返回指定矩阵的交叉乘积。
语法: crossprod(x)
参数:
x: 数字矩阵
例1 :
# R program to illustrate
# crossprod function
# Initializing a matrix with
# 2 rows and 2 columns
x <- matrix(1:4, 2, 2)
# Getting the matrix representation
x
# Calling the crossprod() function
crossprod(x)
输出
[, 1] [, 2]
[1, ] 1 3
[2, ] 2 4
[, 1] [, 2]
[1, ] 5 11
[2, ] 11 25
例2 :
# R program to illustrate
# crossprod function
# Initializing a matrix with
# 3 rows and 3 columns
x <- matrix(1:9, 3, 3)
# Getting the matrix representation
x
# Calling the crossprod() function
crossprod(x)
输出
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9
[, 1] [, 2] [, 3]
[1, ] 14 32 50
[2, ] 32 77 122
[3, ] 50 122 194