-edited为了清晰-
我感兴趣的是求多维自相关函数的零点。
我可以根据我的数据生成自相关矩阵
acm <- autocorr(x, 1:10)然而,整个矩阵可以在20x5000左右,这在计算上是昂贵的。
因此,我想一次只计算1或n行。
以下是我想要采取的步骤
计算matrix
如果这是完整的矩阵:
acm <- cbind( c(10, 9, 8, 7, 6, 5, 4, 3, 1, -1),
c(10, 8, 6, 5, 3, 1, -1, 1, -1, 0 ))我想要一个函数,它将返回10,因为第一个col是最后到达负值的函数。如果我先计算出完整的矩阵,下面的内容就足够了:
max(which(apply(acm, 2, min)))但是我想避免计算更多的acm,例如,因为计算时通常只需要1行或一小部分行。
发布于 2010-11-08 16:55:58
有一个循环解决方案,使用中断函数。这是使用索引和向量tt来跟踪哪些列已经显示负值的攻击。
find.point <- function(x){
tt <- rep(F,ncol(x)) # control vector tt
for (i in 1:nrow(x)){
tt[which(x[i,]<0)] <- T # check which columns have negative value
if(all(tt)) break # if all have reached negative, get out of loop
}
i # return index
}输出与oneliner相同。
max(apply(acm<0,2,function(x) match(T,x)))我相信你在你的问题中提到过。我不太明白你的表现有什么问题。这取决于您是有5000列还是5000行。在任何情况下,即使有一个10倍大的数据集,我也可以在一秒钟内使用oneliner进行计算:
计时:
> acm <- matrix(rep(seq.int(5000,-5999),100),ncol=22)
> dim(acm)
[1] 50000 22
> system.time(max(apply(acm<0,2,function(x) match(T,x))))
user system elapsed
0.05 0.00 0.05
> system.time(find.point(acm))
user system elapsed
0.05 0.00 0.05 然而,当您有许多列时,与oneliner的功能相比,计时功能有了很大的改进:
> acm <- matrix(rep(seq.int(5000,-5999),100),ncol=50000)
> dim(acm)
[1] 22 50000
> system.time(max(apply(acm<0,2,function(x) match(T,x))))
user system elapsed
0.85 0.01 0.86
> system.time(find.point(acm))
user system elapsed
0.03 0.00 0.04 见鬼,你逼我想出一个比单线机更快的循环解决方案。好酷的问题!
发布于 2010-11-05 23:40:58
我不清楚您的函数到底在做什么,但是要回答“如何才能找到动态生成的矩阵的最后一行,其中列的值低于零?”:
findlastzero = function(mat){
apply(mat<0, 2, function(x)tail(which(x),1 ))
}
set.seed(1)
a <- cbind(rnorm(10), rnorm(10), rnorm(10), rnorm(10)) + 0.5
a
[,1] [,2] [,3] [,4]
[1,] -0.1264538 2.0117812 1.41897737 1.85867955
[2,] 0.6836433 0.8898432 1.28213630 0.39721227
[3,] -0.3356286 -0.1212406 0.57456498 0.88767161
[4,] 2.0952808 -1.7146999 -1.48935170 0.44619496
[5,] 0.8295078 1.6249309 1.11982575 -0.87705956
[6,] -0.3204684 0.4550664 0.44387126 0.08500544
[7,] 0.9874291 0.4838097 0.34420449 0.10571005
[8,] 1.2383247 1.4438362 -0.97075238 0.44068660
[9,] 1.0757814 1.3212212 0.02184994 1.60002537
[10,] 0.1946116 1.0939013 0.91794156 1.26317575
findlastzero(a)
[1] 6 4 8 5不过,不知道这是不是你想要的。
发布于 2010-11-06 02:42:57
不确定我是否正确地理解了您的问题,但是您可以使用tapply在矩阵的每一行中提取您想要的信息。
我首先创建一个与您的a大小相同的“分组矩阵”,这是将每一行作为输入输入到lambda函数中的索引。
matrix(rep(1:10,4),nrow=10,ncol=4)然后,我使用分组矩阵在原始矩阵上运行"tapply“。这将设置矩阵,以便将每一行向量传递到函数中:
function(x) { return( x[which(x<0)] ) }它只返回每一行值小于零的所有值。
> a
[,1] [,2] [,3] [,4]
[1,] 0.5341781 -0.9263866 -0.5380141 -1.2453310
[2,] 0.2931630 1.0490300 0.8127472 0.2473263
[3,] 1.0936143 -0.3399709 1.8199833 1.0053080
[4,] 1.0002433 0.2002659 1.7730118 1.7578414
[5,] 0.8116914 0.9371518 0.8727981 1.4236349
[6,] -0.1127914 1.1563594 1.0331311 0.7658510
[7,] -0.5423493 1.8905533 -0.8121652 0.1355076
[8,] -1.6589310 0.4081290 0.3560005 1.6043205
[9,] 1.8760435 0.8826245 1.4457357 0.7561550
[10,] -0.8503400 0.2302597 0.5838986 0.1252952
> matrix(rep(1:10,4),nrow=10,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 3 3 3 3
[4,] 4 4 4 4
[5,] 5 5 5 5
[6,] 6 6 6 6
[7,] 7 7 7 7
[8,] 8 8 8 8
[9,] 9 9 9 9
[10,] 10 10 10 10
> tapply(a, matrix(rep(1:10,4),nrow=10,ncol=4), function(x) { return(x[which(x<0)])})
$`1`
[1] -0.9263866 -0.5380141 -1.2453310
$`2`
numeric(0)
$`3`
[1] -0.3399709
$`4`
numeric(0)
$`5`
numeric(0)
$`6`
[1] -0.1127914
$`7`
[1] -0.5423493 -0.8121652
$`8`
[1] -1.658931
$`9`
numeric(0)
$`10`
[1] -0.85034https://stackoverflow.com/questions/4110937
复制相似问题