【问题标题】:R Code: non negative solution that satisfy n matrix equationsR代码:满足n个矩阵方程的非负解
【发布时间】:2015-12-25 16:47:52
【问题描述】:

这是一个矩阵问题,我试图解决一个满足 2 个或更多矩阵方程的最佳拟合非负 x。我能够单独求解方程,但我不知道如何同时求解所有 'n' 矩阵。

Ax - c = Bx - d = Ex - f = ... = 0

下面我有 2 组我单独求解的矩阵。

Ax = c

By = d

x = y 没有约束

require(pracma)
require(corpcor)
require(NMF)

mat=c(0.005,0.006,0.002,0,0,0,0,
      0,0.005,0.006,0.002,0,0,0,
      0,0,0.005,0.006,0.002,0,0,
      0,0,0,0.005,0.006,0.002,0,
      0,0,0,0,0.005,0.006,0.002,
      0,0,0,0,0,0.005,0.006,
      0.003,0.004,0.002,0,0,0,0,
      0,0.003,0.004,0.002,0,0,0,
      0,0,0.003,0.004,0.002,0,0,
      0,0,0,0.003,0.004,0.002,0,
      0,0,0,0,0.003,0.004,0.002,
      0,0,0,0,0,0.003,0.004     
)
mat = matrix(mat,byrow=T,ncol=7)
rownames(mat) = rep(c("Group A", "Group B"), times = c(6,6))

mat.A = mat[rownames(mat) == "Group A",]
mat.B = mat[rownames(mat) == "Group B",]

y.A = sample(c(100:500), 7)
y.B = sample(c(200:300), 7)

# Solve B
a = t(mat.A)
b = as.numeric(y.A)

Test.a  = qr(a, tol = 0.0000001)
nc = ncol(Test.a$qr)
nr = nrow(Test.a$qr)
if (Test.a$rank != min(nc, nr)){
  x = ceiling(fcnnls(a,b, pseudo = T)$x)
} else x = ceiling(lsqnonneg(a,b)$x)

# Solve A 
a = t(mat.B)
b = as.numeric(y.B)

Test.a  = qr(a, tol = 0.0000001)
nc = ncol(Test.a$qr)
nr = nrow(Test.a$qr)
if (Test.a$rank != min(nc, nr)){
  x = ceiling(fcnnls(a,b, pseudo = T)$x)
} else x = ceiling(lsqnonneg(a,b)$x)

【问题讨论】:

    标签: r math matrix


    【解决方案1】:

    我们假设“最佳拟合”意味着找到最小化的非负x

    ||Ax - c||2 + ||Bx - d||2

    我们可以使用 nnls 包进行计算。假设mat 是由堆叠在B 行之上的A 行组成的矩阵,即rbind(A, B),并且cd 都是向量,所以@987654328 @ 是我们拥有的nrow(mat) 的向量:

    library(nnls)
    nnls(mat, rep(1, nrow(mat)))
    

    给予:

    Nonnegative least squares model
    x estimates: 82.87176 83.51637 104.6671 52.97634 148.3001 0 193.7866 
    residual sum-of-squares: 0.39
    reason terminated: The solution has been computed sucessfully.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 2022-07-30
      • 2012-05-02
      • 2020-08-18
      • 1970-01-01
      • 2013-08-10
      • 2014-09-04
      • 1970-01-01
      相关资源
      最近更新 更多