给定100x100矩阵,我们想要计算一个20x20矩阵,它的每个单元代表原始矩阵的5x5平方的平均值。
如何表演? (如果您对此操作有任何更好的名称,请注释重命名问题。)
答案 0 :(得分:5)
以下是几个选项。
直接的方法是使用栅格包中的aggregate()
:
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
对于更优雅或模糊的方法(取决于您的观点),使用几个矩阵乘法:
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
答案 1 :(得分:4)
让我做一个小玩具示例:
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
这样,对于6 * 6矩阵,计算每个2 * 2块矩阵,我们得到一个3 * 3汇总矩阵。
答案 2 :(得分:0)
晚会,但是你走了:
# 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)
)