R:将矩阵拆分为任意数量的块 [英] R: split matrix into arbitrary number of blocks
问题描述
说我有一个值矩阵
set.seed(1)
A <- matrix(runif(25),ncol=5)
我想为这个大小相等的矩阵内的近似正方形邻域计算一些统计信息.这些输出中的任何一种都可以:
I'd like to calculate some statistics for approximately square neighborhoods within this matrix of approximately equal size. Either of these kinds of output would do:
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()
来计算统计信息,如下所示:
other approximations are also OK, since I can always rotate the matrix. Then I can use these neighborhood matrices to calculate stats using tapply()
, like this:
tapply(A,N1,mean)
A B C D
0.6201744 0.5057402 0.4574495 0.5594227
我想要的是一个函数,该函数可使我具有任意维度的矩阵,并具有任意数量的块状邻域,例如N1
或N2
.我很难弄清楚这种功能如何处理所需的块数甚至不是平方的情况. N1
和N2
具有4个邻域,但是说我想要5个类似这样的输出:
What I want is a function that can make me a matrix of arbitrary dimensions with an arbitrary number of block-like neighborhoods like N1
or N2
. I'm having a hard time trying to figure out how such a function would deal with situations where the desired number of blocks are not even squares. N1
and N2
have 4 neighborhoods, but say I wanted 5 for some output something like this:
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"
是否有人知道可以进行这种拆分的现有功能,或者对如何进行拆分有任何想法?谢谢!
Does anyone know of an existing function that can do this kind of split, or have any ideas on how to make one? Thank you!
[] 考虑到文森特的建议,我的最后职责是:
[] My final function, taking into account Vincent's advice:
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)
推荐答案
您可以尝试使用row
和col
函数:
他们将问题减少到一维问题.
以下定义了大小最大为2 * 2的块.
You can try to play with the row
and col
functions:
they reduce the problem to a 1-dimensional one.
The following defines blocks of size at most 2*2.
matrix(
paste(
ceiling(col(A)/2),
ceiling(row(A)/2),
sep="-"),
nc=ncol(A)
)
这篇关于R:将矩阵拆分为任意数量的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!