如何用线性独立列来写矩阵中的线性相关列? [英] How to write linearly dependent column in a matrix in terms of linearly independent columns?

查看:300
本文介绍了如何用线性独立列来写矩阵中的线性相关列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的mxn矩阵,并且已经确定了线性相关的列.但是,我想知道R中是否有一种方法可以根据线性独立的列来编写线性相关的列.由于矩阵很大,因此无法基于检查来完成.

I have a large mxn matrix, and I have identified the linearly dependent columns. However, I want to know if there's a way in R to write the linearly dependent columns in terms of the linearly independent ones. Since it's a large matrix, it's not possible to do based on inspection.

这是我拥有的矩阵类型的一个玩具示例.

Here's a toy example of the type of matrix I have.

> 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.我想知道是否存在一种自动方法来获取更大的矩阵.

Here it's obvious that x3 = x1-x2, x5=x1-x4. I want to know if there's an automated way to get that for a larger matrix.

谢谢!

推荐答案

我确定有更好的方法,但是我想尝试一下.我基本上会在开始时进行检查,以查看输入矩阵是否为完整列级别,以避免在其为完整列时进行不必要的计算.之后,我从前两列开始,检查该子矩阵是否具有完整的列等级,如果是,则检查第一列,依此类推.一旦找到不全列级别的子矩阵,我就将前一个子矩阵中的最后一列进行回归,从而告诉我们如何构造第一列的线性组合以获得最后一列.

I'm sure there is a better way but I felt like playing around with this. I basically do a check at the beginning to see if the input matrix is full column rank to avoid unnecessary computation in case it is full rank. After that I start with the first two columns and check if that submatrix is of full column rank, if it is then I check the first thee columns and so on. Once we find some submatrix that isn't of full column rank I regress the last column in that submatrix on the previous one which tells us how to construct linear combinations of the first columns to get the last column.

我的功能目前还不是很干净,可以进行一些其他检查,但这至少是一个开始.

My function isn't very clean right now and could do some additional checking but at least it's a start.

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"

这篇关于如何用线性独立列来写矩阵中的线性相关列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆