【问题标题】:How to write linearly dependent column in a matrix in terms of linearly independent columns?如何根据线性独立列在矩阵中写入线性相关列?
【发布时间】:2012-10-26 14:23:45
【问题描述】:

我有一个很大的 mxn 矩阵,并且我已经确定了线性相关的列。但是,我想知道 R 中是否有一种方法可以根据线性独立列来编写线性相关列。由于是大矩阵,所以不能根据检查来做。

这是我所拥有的矩阵类型的玩具示例。

> mat <- matrix(c(1,1,0,1,0,1,1,0,0,1,1,0,1,1,0,1,0,1,0,1), byrow=TRUE, ncol=5, nrow=4)
> mat
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    0    1    0
[2,]    1    1    0    0    1
[3,]    1    0    1    1    0
[4,]    1    0    1    0    1

这里很明显 x3 = x1-x2, x5=x1-x4。我想知道是否有一种自动化的方法来获得更大的矩阵。

谢谢!

【问题讨论】:

标签: r matrix linear-algebra


【解决方案1】:

我确信有更好的方法,但我想玩弄这个。我基本上在开始时检查输入矩阵是否是满列秩,以避免在满秩的情况下进行不必要的计算。之后,我从前两列开始并检查该子矩阵是否具有满列等级,如果是,则检查前列,依此类推。一旦我们找到一些不是满列等级的子矩阵,我会将该子矩阵中的最后一列回归到前一列,这告诉我们如何构造第一列的线性组合以获得最后一列。

我的函数现在不是很干净,可以做一些额外的检查,但至少这是一个开始。

mat <- matrix(c(1,1,0,1,0,1,1,0,0,1,1,0,1,1,0,1,0,1,0,1), byrow=TRUE, ncol=5, nrow=4)


linfinder <- function(mat){
    # If the matrix is full rank then we're done
    if(qr(mat)$rank == ncol(mat)){
        print("Matrix is of full rank")
        return(invisible(seq(ncol(mat))))
    }
    m <- ncol(mat)
    # cols keeps track of which columns are linearly independent
    cols <- 1
    for(i in seq(2, m)){
        ids <- c(cols, i)
        mymat <- mat[, ids]
        if(qr(mymat)$rank != length(ids)){
            # Regression the column of interest on the previous
            # columns to figure out the relationship
            o <- lm(mat[,i] ~ mat[,cols] + 0)
            # Construct the output message
            start <- paste0("Column_", i, " = ")
            # Which coefs are nonzero
            nz <- !(abs(coef(o)) <= .Machine$double.eps^0.5)
            tmp <- paste("Column", cols[nz], sep = "_")
            vals <- paste(coef(o)[nz], tmp, sep = "*", collapse = " + ")
            message <- paste0(start, vals)
            print(message)
        }else{
            # If the matrix subset was of full rank
            # then the newest column in linearly independent
            # so add it to the cols list
            cols <- ids
        }
    }
    return(invisible(cols))
}

linfinder(mat)

给了

> linfinder(mat)
[1] "Column_3 = 1*Column_1 + -1*Column_2"
[1] "Column_5 = 1*Column_1 + -1*Column_4"

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
相关资源
最近更新 更多