简体   繁体   English

如何在R编程中手动查找矩阵的次要?

[英]How to manually find the minors of a matrix in R programming?

I am supposed to create a function that returns all the minors of a given matrix.我应该创建一个函数来返回给定矩阵的所有次要矩阵。 I use a for loop to delete the necessary row/column but every time it iterates, it just writes over the previous minor.我使用 for 循环来删除必要的行/列,但每次迭代时,它只会覆盖前一个次要。 How can I keep a copy of all the minors?我如何保留所有未成年人的副本? This is what I have so far in the function: (Suppose that b is my given matrix)这是我到目前为止在函数中的内容:(假设 b 是我给定的矩阵)

minors <- function(b, i, j){
  for(i in 1: nrow(b)){
    for(j in 1: ncol(b)){
      a = b[-i,-j]
    }
  }
  a
}

b = matrix(c(2,3,5,6,7,1,9,4,5), nrow = 3, ncol = 3)
minors(b, i, j)

I'd like to keep the function as simple as possible with just loops.我想通过循环使函数尽可能简单。 It's also important that I have a way to access the minors individually as I'll be getting each of their cofactors later on.同样重要的是,我有一种方法可以单独访问未成年人,因为稍后我将获得他们的每个辅因子。

Any help/tips/advice will be greatly appreciated!!任何帮助/提示/建议将不胜感激!!

If you want all the minors, you could use a double for loop as shown below:如果你想要所有的未成年人,你可以使用双 for 循环,如下所示:

minors <- function(b){
  n <- nrow(b)
  a <- matrix(NA, n, n)
  for(i in 1:n)
    for(j in 1:n)
      a[i, j] = det(b[-i, -j])
    a
}
b = matrix(c(2,3,5,6,7,1,9,4,5), nrow = 3, ncol = 3)
minors(b) 
    [,1] [,2] [,3]
[1,]   31   -5  -32
[2,]   21  -35  -28
[3,]  -39  -19   -4

cofactors <- function(b) (-1)^(row(b)+col(b)) *minors(b)
cofactors(b)
     [,1] [,2] [,3]
[1,]   31    5  -32
[2,]  -21  -35   28
[3,]  -39   19   -4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM