gpt4 book ai didi

r - 在R中获取伴随矩阵

转载 作者:行者123 更新时间:2023-12-01 10:53:28 24 4
gpt4 key购买 nike

矩阵的第 (i,j) 次矩阵是删除了第 i 行和第 j 列的矩阵。

minor <- function(A, i, j)
{
A[-i, -j]
}

第 (i,j) 个余因子是第 (i,j) 个小次 -1 的 i + j 次方。

cofactor <- function(A, i, j)
{
-1 ^ (i + j) * minor(A, i, j)
}

通过这种方式,我得到了A的余因子,那么我怎样才能得到伴随矩阵呢?

最佳答案

-1 两边需要括号和 minor 定义中的行列式.

之后,您可以使用循环或outer

# Sample data
n <- 5
A <- matrix(rnorm(n*n), n, n)

# Minor and cofactor
minor <- function(A, i, j) det( A[-i,-j] )
cofactor <- function(A, i, j) (-1)^(i+j) * minor(A,i,j)

# With a loop
adjoint1 <- function(A) {
n <- nrow(A)
B <- matrix(NA, n, n)
for( i in 1:n )
for( j in 1:n )
B[j,i] <- cofactor(A, i, j)
B
}

# With `outer`
adjoint2 <- function(A) {
n <- nrow(A)
t(outer(1:n, 1:n, Vectorize(
function(i,j) cofactor(A,i,j)
)))
}

# Check the result: these should be equal
det(A) * diag(nrow(A))
A %*% adjoint1(A)
A %*% adjoint2(A)

关于r - 在R中获取伴随矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16757100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com