Hello, I fixed a small bug in case some one wants to source it.

B.R
Alex


# A) My for loop version as a function

do_sliding_for_a_window_duty_cycle <- function(DataToAnalyse, threshold, 
windowSize) {
  data=matrix(data=NA,nrow=nrow(ThresholdData),ncol=ncol(ThresholdData))
  ThresholdData <- (DataToAnalyse > threshold)
  data[ThresholdData==TRUE]=1
  data[ThresholdData==FALSE]=0


  out <- numeric()
  elements<- numeric()
  if (length(data[,1]) > windowSize){
      for (i in 1:(length(data[,1]) - windowSize +1  )) {
        out[i] <- mean(data[i:(i + windowSize - 1), ])
        elements[i]<-length(i:(i + windowSize - 1))
      }
  }
  
  return (list(result=out , numberOfElements=elements, windowSize=windowSize ))
} 


# B) My equivalent I tried (before puting into a function by using runmean)
require(caTools)
DataToAnalyse<-matrix(seq(1:10000),nrow=100,byrow=TRUE)
ThresholdData<-matrix(data=NA,nrow(DataToAnalyse),ncol(DataToAnalyse))
ThresholdDataBoolean<-( DataToAnalyse > 5000)
ThresholdData[ThresholdDataBoolean==TRUE]=1
ThresholdData[ThresholdDataBoolean==FALSE]=0

keep1<-(runmean((ThresholdData),90,alg="R",endrule="trim"))
keep1<-rowSums(keep1)/90



keep2<-do_sliding_for_a_window_duty_cycle(DataToAnalyse,5000,90)



# Now if one compares the two results
keep1-keep2$result





will see that are not alike....

What might the problem here?

I would like to thank you in advance

B.R
Alex









ust move it to C and you'll probably be ok. I believe runmean in 
library(caTools) provides a very fast implementation. 

Michael

On Mar 3, 2012, at 8:31 AM, Alaios <ala...@yahoo.com> wrote:

> Dear all,
> I am having a vector of around 300.000 elements and I Want to slide fast a 
> window from the first element until the last-Windowsize
> 
> what I have so far is the following for statement:
> 
> �for (i in 1:(length(data[,1]) - windowSize)) {
> ��� ��� out[i] <- mean(data[i:(i + windowSize - 1), ])
> ��� ��� elements[i]<-length(i:(i + windowSize - 1))
> ��� � }
> 
> but this of course takes ages to run, especially with small window sizes!.
> Is it possible to speed up this in many cores in R? If yes how?
> 
> I would like to thank you in advance for your help
> 
> B.R
> Alex
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
    [[alternative HTML version deleted]]


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to