R:将矩阵分割成任意数量的块

时间:2012-02-24 12:41:18

标签: r matrix block

假设我有一个值矩阵

    set.seed(1)
    A <- matrix(runif(25),ncol=5)

我想在这个大小相等的矩阵中计算一些近似正方形邻域的统计数据。这些输出中的任何一种都可以:

    N1 <-  matrix(c(rep(c("A","A","B","B","B"),2),rep(c("C","C","D","D","D"),3)),ncol=5)
    N2 <-  matrix(c(rep(c("A","A","A","B","B"),3),rep(c("C","C","D","D","D"),2)),ncol=5)
    N1
         [,1] [,2] [,3] [,4] [,5]
    [1,] "A"  "A"  "C"  "C"  "C" 
    [2,] "A"  "A"  "C"  "C"  "C" 
    [3,] "B"  "B"  "D"  "D"  "D" 
    [4,] "B"  "B"  "D"  "D"  "D" 
    [5,] "B"  "B"  "D"  "D"  "D" 

    N2
         [,1] [,2] [,3] [,4] [,5]
    [1,] "A"  "A"  "A"  "C"  "C" 
    [2,] "A"  "A"  "A"  "C"  "C" 
    [3,] "A"  "A"  "A"  "D"  "D" 
    [4,] "B"  "B"  "B"  "D"  "D" 
    [5,] "B"  "B"  "B"  "D"  "D" 

其他近似值也可以,因为我总是可以旋转矩阵。然后我可以使用这些邻域矩阵来使用tapply()计算统计数据,如下所示:

    tapply(A,N1,mean)
            A         B         C         D 
    0.6201744 0.5057402 0.4574495 0.5594227

我想要的是一个函数,它可以使我成为任意维度的矩阵,具有任意数量的类似块的邻域,如N1N2。我很难弄清楚这样的函数如何处理所需数量的块甚至不是正方形的情况。 N1N2有4个社区,但我想要输出5个像这样的输出:

    N3 <-  matrix(c("A","A","B","B","B","A","A","C","C","C","D","D","C","C","C",
            "D","D","E","E","E","D","D","E","E","E"),ncol=5)
         [,1] [,2] [,3] [,4] [,5]
    [1,] "A"  "A"  "D"  "D"  "D" 
    [2,] "A"  "A"  "D"  "D"  "D" 
    [3,] "B"  "C"  "C"  "E"  "E" 
    [4,] "B"  "C"  "C"  "E"  "E" 
    [5,] "B"  "C"  "C"  "E"  "E" 

有没有人知道可以进行这种拆分的现有功能,或者对如何制作这种功能有任何想法?谢谢!

[[编辑]] 我的最后一项功能,考虑到文森特的建议:

    DecideBLocks <- function(A,nhoods){
        nc <- ncol(A)
        nr <- nrow(A)
        nhood_side <- floor(sqrt((nc*nr)/nhoods))
        Neighborhoods <- matrix(paste(ceiling(col(A)/nhood_side), ceiling(row(A)/nhood_side), sep="-"), nc=ncol(A)) 
        nhoods.out <- length(unique(c(Neighborhoods)))
        if (nhoods.out != nhoods){
            cat(nhoods.out,"neighborhoods created.\nThese were on average",nhood_side,"by",nhood_side,"cells\nit's a different number than that stated the function tries to round things to square neighborhoods\n")
        }
        return(Neighborhoods)
    }
    A <- matrix(rnorm(120),12)
    B <- DecideBLocks(A,13)

2 个答案:

答案 0 :(得分:2)

您可以尝试使用rowcol功能: 他们将问题简化为一维问题。 以下定义的块大小最多为2 * 2.

matrix( 
  paste(
    ceiling(col(A)/2), 
    ceiling(row(A)/2), 
    sep="-"), 
  nc=ncol(A) 
)

答案 1 :(得分:1)

您可以以任何您喜欢的方式选择靠近youree矩阵维度中心的bdeep(行规格)和bwide(共规格)参数,并使用此简单函数构建矩阵。只要bwide和bdeep相等,并且nrow == ncol,你就应该得到方形子矩阵。

 mkblk <- function(bwide, bdeep, nrow, ncol){
   bstr1 <- c(rep("A", bdeep), rep("B", nrow-bdeep))
   bstr2 <- c(rep("C", bdeep), rep("D", nrow-bdeep))
   matrix(c( rep(bstr1, bwide), rep(bstr2, ncol-bwide)), ncol=ncol, nrow=nrow)}
 mkblk(2,2,5,5)

     [,1] [,2] [,3] [,4] [,5]
[1,] "A"  "A"  "C"  "C"  "C" 
[2,] "A"  "A"  "C"  "C"  "C" 
[3,] "B"  "B"  "D"  "D"  "D" 
[4,] "B"  "B"  "D"  "D"  "D" 
[5,] "B"  "B"  "D"  "D"  "D" 

#Test of your strategy
 tapply(A, mkblk(2,2,5,5), mean)
        A         B         C         D 
0.6201744 0.5057402 0.4574495 0.5594227