R函数创建带状矩阵

发布于 2024-10-20 08:00:16 字数 1614 浏览 0 评论 0原文

我需要编写两个函数 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*"

I need to write two functions toBand() and replaceBand():

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

to get the output below.

The first returns a matrix of the diagonal and co-diagonal elements, using the lower triangle of X. The second replaces the corresponding elements in X from the data in the band matrix supplied. The function then returns the modified X.

Are there any packages that I can use to do this? Any suggestions on how to do this?

Thank you

> 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*"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜血染红嫁衣 2024-10-27 08:00:16

如果您不需要使用 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*"

以下是 toBand 和 < code>replaceBand 按描述工作。我想通过算术来弄清楚如何填充矩阵会更干净,但这是一种无需费力思考即可完成的方法。或许别人也会这样回答。

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
}

This is straightforward if you don't need to use the output from toBand:

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

Making the matrices to demonstrate:

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)

The output is exactly as desired:

> 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*"

Here are versions of toBand and replaceBand that work as described. I imagine it would be cleaner to do the arithmetic to figure out exactly how to fill in the matrices, but this is a way to do it without having to think very hard. Perhaps someone else will answer it that way.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文