这是示例代码
# sample data
N <- 100
s <- matrix(rexp(1000000), 10000)
sif <- matrix(0,1,N)
count <- 0
# the slow for loop
for(ii in 1:(round(length(s)/N)-1))
{
# incdex counter for final vector
count <- count + 1
# populates new matrix with a range from the s matrix
sif[count,] <- s[(1+((ii-1)*N)):(ii*N)]
# stacks new row onto matrix
sif <- rbind(sif, (count + 1))
}
表现
矩阵中有 100 万个元素,性能相当缓慢。有人知道如何对上述样本进行矢量化吗?