[R] efficient submatrix extraction

Tony Plate tplate at blackmesacapital.com
Wed Sep 15 23:33:27 CEST 2004


I think you should be able to do something with reassigning the "dim" 
attribute, and then using apply(), something along the lines of the 
following (which doesn't do your computation on the data in the subarrays, 
but merely illustrates how to create and access them):

 > x <- matrix(1:64,ncol=8)
 > x
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    9   17   25   33   41   49   57
[2,]    2   10   18   26   34   42   50   58
[3,]    3   11   19   27   35   43   51   59
[4,]    4   12   20   28   36   44   52   60
[5,]    5   13   21   29   37   45   53   61
[6,]    6   14   22   30   38   46   54   62
[7,]    7   15   23   31   39   47   55   63
[8,]    8   16   24   32   40   48   56   64
 > x2 <- x
 > dim(x2) <- c(2,4,2,4)
 > x2[,1,,1]
      [,1] [,2]
[1,]    1    9
[2,]    2   10
 > x2[,2,,1]
      [,1] [,2]
[1,]    3   11
[2,]    4   12
 > x2[,1,,2]
      [,1] [,2]
[1,]   17   25
[2,]   18   26
 > x4 <- x
 > dim(x4) <- c(4,2,4,2)
 > x4[,1,,1]
      [,1] [,2] [,3] [,4]
[1,]    1    9   17   25
[2,]    2   10   18   26
[3,]    3   11   19   27
[4,]    4   12   20   28
 > invisible(apply(x4, c(2,4), print))
      [,1] [,2] [,3] [,4]
[1,]    1    9   17   25
[2,]    2   10   18   26
[3,]    3   11   19   27
[4,]    4   12   20   28
      [,1] [,2] [,3] [,4]
[1,]    5   13   21   29
[2,]    6   14   22   30
[3,]    7   15   23   31
[4,]    8   16   24   32
      [,1] [,2] [,3] [,4]
[1,]   33   41   49   57
[2,]   34   42   50   58
[3,]   35   43   51   59
[4,]   36   44   52   60
      [,1] [,2] [,3] [,4]
[1,]   37   45   53   61
[2,]   38   46   54   62
[3,]   39   47   55   63
[4,]   40   48   56   64
 >

hope this helps,

Tony Plate

At Wednesday 03:10 PM 9/15/2004, Rajarshi Guha wrote:
>Hi,
>   I have a matrix of say 1024x1024 and I want to look at it in chunks.
>That is I'd like to divide into a series of submatrices of order 2x2.
>
>| 1 2 3 4 5 6 7 8 ... |
>| 1 2 3 4 5 6 7 8 ... |
>| 1 2 3 4 5 6 7 8 ... |
>| 1 2 3 4 5 6 7 8 ... |
>...
>
>So the first submatrix would be
>
>| 1 2 |
>| 1 2 |
>
>the second one would be
>
>| 3 4 |
>| 3 4 |
>
>and so on. That is I want the matrix to be evenly divided into 2x2
>submatrices. Now I'm also doing this subdivision into 4x4, 8x8 ...
>256x256 submatrices.
>
>Currently I'm using loops and I'm sure there is a mroe efficient way to
>do it:
>
>     m <- matrix(runif(1024*1024), nrow=1024)
>     boxsize <- 2^(1:8)
>
>     for (b in boxsize) {
>         bcount <- 0
>         bstart <- seq(1,1024, by=b)
>         for (x in bstart) {
>             for (y in bstart) {
>                 xend <- x + b - 1
>                 yend <- y + b - 1
>                 if (length(which( m[ x:xend, y:yend ] > 0.7)) > 0) {
>                     bcount <- bcount + 1
>                 }
>             }
>         }
>     }
>
>Is there any way to vectorize the two inner loops?
>
>Thanks,
>
>-------------------------------------------------------------------
>Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
>GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
>-------------------------------------------------------------------
>The way to love anything is to realize that it might be lost.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list