Ali
Ali

Reputation: 9830

Matrix scale-out

Given a 100x100 matrix, we want to compute a 20x20 matrix, every cell of it represents the mean of a 5x5 square of the original matrix.

How to perform it? (If you have any better name for this operation please comment to rename question).

Upvotes: 3

Views: 189

Answers (3)

thelatemail
thelatemail

Reputation: 93803

Late to the party, but here you go:

# width/height of the sub-squares
side <- 5
# test matrix
test <- outer(1:100,1:100)
# make a selection matrix categorising each cell
select <- matrix(
                  rep(1:(length(test)/(side^2)),each=side),
                  nrow=nrow(test)
                )[,rep(1:(ncol(test)/side),each=side)]
# get the result
matrix(
        sapply(1:(length(test)/(side^2)),function(x) mean(test[select==x])),
        nrow=(ncol(test)/side)
      )

Upvotes: 0

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162311

Here are a couple of options.

The straightforward approach is to use aggregate() from the raster package:

m <- matrix(1:10000, ncol=100)

library(raster)
r <- raster(m)
as.matrix(aggregate(r, 5))

## aggregate() also supports non-square aggregation windows
as.matrix(aggregate(r, c(20, 50)))
#        [,1]   [,2]   [,3]   [,4]   [,5]
# [1,]  975.5 2975.5 4975.5 6975.5 8975.5
# [2,] 1025.5 3025.5 5025.5 7025.5 9025.5

For a more elegant or obfuscated approach (depending on your point of view) use a couple of matrix multiplications:

m <- matrix(1:10000, ncol=100)

mm <- suppressWarnings(matrix(rep(c(1, rep(0, 20)), each=5), ncol=20, nrow=100))
(t(mm) %*% m %*% mm)/25

Upvotes: 5

liuminzhao
liuminzhao

Reputation: 2455

Let me do a small toy example:

R > mat = matrix(1:36, 6,6)
R > mat
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    7   13   19   25   31
[2,]    2    8   14   20   26   32
[3,]    3    9   15   21   27   33
[4,]    4   10   16   22   28   34
[5,]    5   11   17   23   29   35
[6,]    6   12   18   24   30   36

R > A = matrix(paste(ceiling(col(mat)/2), ceiling(row(mat)/2), sep = "-"), nc = ncol(mat))
R > A
     [,1]  [,2]  [,3]  [,4]  [,5]  [,6] 
[1,] "1-1" "1-1" "2-1" "2-1" "3-1" "3-1"
[2,] "1-1" "1-1" "2-1" "2-1" "3-1" "3-1"
[3,] "1-2" "1-2" "2-2" "2-2" "3-2" "3-2"
[4,] "1-2" "1-2" "2-2" "2-2" "3-2" "3-2"
[5,] "1-3" "1-3" "2-3" "2-3" "3-3" "3-3"
[6,] "1-3" "1-3" "2-3" "2-3" "3-3" "3-3"

R > matrix(tapply(mat, A, mean), 3, 3)
     [,1] [,2] [,3]
[1,]  4.5 16.5 28.5
[2,]  6.5 18.5 30.5
[3,]  8.5 20.5 32.5

This way, for a 6*6 matrix, computing each 2*2 block matrix, we get a 3*3 summary matrix.

Reference

Upvotes: 4

Related Questions