我在R中有一个大的矩阵:
norMat <- matrix(NA, nrow=1024, ncol=1024)该空矩阵需要用所有矩阵索引对的所有相等位的总和填充。
所以我需要为i (rowIndex)和j (colIndex)计算逻辑NOR并对结果求和,例如:
sum(intToBits(2)==intToBits(3))Currenty,我有这个函数填充矩阵:
norMatrix <- function() { matDim=1024 norMat <<- matrix(NA, nrow=matDim, ncol=matDim) for(i in 0:(matDim-1)) { for(j in 0:(matDim-1)) { norMat[i+1,j+1] = norsum(i,j) } } return(norMat) }这是norsum功能:
norsum <- function(bucket1, bucket2) { res = sum(intToBits(bucket1)==intToBits(bucket2)) return(res) }这是填充矩阵的有效解决方案吗? 我有疑问,因为在我的机器上这需要5分钟。
I have a big square matrix in R:
norMat <- matrix(NA, nrow=1024, ncol=1024)This empty matrix needs to be filled with the sum of all equal bits of all matrix index pairs.
So I need to calculate the logical NOR for i(rowIndex) and j(colIndex) and sum the result,e.g:
sum(intToBits(2)==intToBits(3))Currenty, I have this function which fills the matrix:
norMatrix <- function() { matDim=1024 norMat <<- matrix(NA, nrow=matDim, ncol=matDim) for(i in 0:(matDim-1)) { for(j in 0:(matDim-1)) { norMat[i+1,j+1] = norsum(i,j) } } return(norMat) }And here's the norsum function:
norsum <- function(bucket1, bucket2) { res = sum(intToBits(bucket1)==intToBits(bucket2)) return(res) }Is this an efficient solution to fill the matrix? I'm in doubt since on my machine this takes over 5 minutes.
最满意答案
我建议这是*apply函数的一个很好的机会。 这是一个比5分钟快一点的解决方案。
首先,概念证明,非正方形仅仅是为了清晰度。
nc <- 5 nr <- 6 mtxi <- sapply(seq_len(nc), intToBits) mtxj <- sapply(seq_len(nr), intToBits) sapply(1:nc, function(i) sapply(1:nr, function(j) sum(mtxi[,i] == mtxj[,j]))) # [,1] [,2] [,3] [,4] [,5] # [1,] 32 30 31 30 31 # [2,] 30 32 31 30 29 # [3,] 31 31 32 29 30 # [4,] 30 30 29 32 31 # [5,] 31 29 30 31 32 # [6,] 29 31 30 31 30假设这些是正确的,全餐交易:
n <- 1024 mtx <- sapply(seq_len(n), intToBits) system.time( ret <- sapply(1:n, function(i) sapply(1:n, function(j) sum(mtx[,i] == mtx[,j]))) ) # user system elapsed # 3.25 0.00 3.36从技术上讲,您不需要预先计算mtxi和mtxj 。 虽然intToBits没有引入太多开销,但我认为每次重新计算都是愚蠢的。
我的系统是合理的(i7 6600U CPU @ 2.60GHz),win10_64,R-3.3.2 ...没什么太花哨的。
I suggest this is a great opportunity for the *apply functions. Here's one solution that's a bit faster than 5 minutes.
First, proof of concept, non-square solely for clarity of dimensions.
nc <- 5 nr <- 6 mtxi <- sapply(seq_len(nc), intToBits) mtxj <- sapply(seq_len(nr), intToBits) sapply(1:nc, function(i) sapply(1:nr, function(j) sum(mtxi[,i] == mtxj[,j]))) # [,1] [,2] [,3] [,4] [,5] # [1,] 32 30 31 30 31 # [2,] 30 32 31 30 29 # [3,] 31 31 32 29 30 # [4,] 30 30 29 32 31 # [5,] 31 29 30 31 32 # [6,] 29 31 30 31 30Assuming that these are correct, the full meal deal:
n <- 1024 mtx <- sapply(seq_len(n), intToBits) system.time( ret <- sapply(1:n, function(i) sapply(1:n, function(j) sum(mtx[,i] == mtx[,j]))) ) # user system elapsed # 3.25 0.00 3.36You don't technically need to pre-calculate mtxi and mtxj. Though intToBits does not introduce much overhead, I think it's silly to recalculate every time.
My system is reasonable (i7 6600U CPU @ 2.60GHz), win10_64, R-3.3.2 ... nothing too fancy.
更多推荐
发布评论