R函数创建带矩阵

时间:2011-03-03 04:47:12

标签: r matrix

我需要编写两个函数toBand()replaceBand()

test3 <- toBand(test1,3)
test4 <- replaceBand(test1, toBand(test2,3))

获取下面的输出。

第一个使用X的下三角形返回对角线和共同对角元素的矩阵。第二个从提供的带矩阵中的数据中替换X中的相应元素。然后该函数返回修改后的X。

我可以使用任何包来做这件事吗?有关如何做到这一点的任何建议吗?

谢谢

> test1
     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
[1,] "a11" "a21" "a31" "a41" "a51" "a61"
[2,] "a21" "a22" "a32" "a42" "a52" "a62"
[3,] "a31" "a32" "a33" "a43" "a53" "a63"
[4,] "a41" "a42" "a43" "a44" "a54" "a64"
[5,] "a51" "a52" "a53" "a54" "a55" "a65"
[6,] "a61" "a62" "a63" "a64" "a65" "a66"
> test2
     [,1]    [,2]    [,3]    [,4]    [,5]    [,6]  
[1,] "*a11*" "*a21*" "*a31*" "*a41*" "*a51*" "*a61*"
[2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "*a62*"
[3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
[4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
[5,] "*a51*" "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
[6,] "*a61*" "*a62*" "*a63*" "*a64*" "*a65*" "*a66*"
> test3
     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
[1,] "a11" "a22" "a33" "a44" "a55" "a66"
[2,] "a21" "a32" "a43" "a54" "a65" NA  
[3,] "a31" "a42" "a53" "a64" NA    NA  
[4,] "a41" "a52" "a63" NA    NA    NA  
> test4
     [,1]    [,2]    [,3]    [,4]    [,5]    [,6]  
[1,] "*a11*" "*a21*" "*a31*" "*a41*" "a51"   "a61" 
[2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "a62" 
[3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
[4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
[5,] "a51"   "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
[6,] "a61"   "a62"   "*a63*" "*a64*" "*a65*" "*a66*"

1 个答案:

答案 0 :(得分:2)

如果您不需要使用toBand的输出:

,这很简单
replaceBand <- function(a, b, k) {
  swap <- abs(row(a) - col(a)) <= k
  a[swap] <- b[swap]
  a
}

制作矩阵以证明:

test1 <- matrix(ncol=6, nrow=6)
test1 <- matrix(paste("a", row(test1), col(test1), sep=""), nrow=6)
test1b <- matrix(paste("a", col(test1), row(test1), sep=""), nrow=6)
test1[upper.tri(test1)] <- test1b[upper.tri(test1b)]
test2 <- matrix(paste("*", test1, "*", sep=""), nrow=6)

输出完全符合要求:

> replaceBand(test1, test2, 3)

     [,1]    [,2]    [,3]    [,4]    [,5]    [,6]   
[1,] "*a11*" "*a21*" "*a31*" "*a41*" "a51"   "a61"  
[2,] "*a21*" "*a22*" "*a32*" "*a42*" "*a52*" "a62"  
[3,] "*a31*" "*a32*" "*a33*" "*a43*" "*a53*" "*a63*"
[4,] "*a41*" "*a42*" "*a43*" "*a44*" "*a54*" "*a64*"
[5,] "a51"   "*a52*" "*a53*" "*a54*" "*a55*" "*a65*"
[6,] "a61"   "a62"   "*a63*" "*a64*" "*a65*" "*a66*"

以下是toBandreplaceBand的版本,其工作原理如上所述。我认为用算法弄清楚如何填充矩阵会更干净,但这是一种方法,而不必非常努力地思考。也许其他人会这样回答。

toBand <- function(x,k) {
  n <- nrow(x)
  out <- matrix(nrow=n, ncol=n)
  out[row(out) + col(out) - 1 <= n] <- x[lower.tri(x, diag=TRUE)]
  out[1:(k+1),]
}

replaceBand <- function(a, b) {
  b[row(b)+col(b)-1 <= ncol(b)]
  swap <- abs(row(a) - col(a)) <= nrow(b) - 1
  a[swap & lower.tri(a, diag=TRUE)] <- b[row(b)+col(b)-1 <= ncol(b)]
  a[upper.tri(a)] <- t(a)[upper.tri(a)]
  a
}