On 12-08-28 5:45 PM, Rui Barradas wrote:
Hello,

Try the following.

occur1 <- function(pat, vec){
      m <- length(pat)
      n <- length(vec)
      candidate <- seq.int(length=n-m+1)
      for (i in seq.int(length=m))
          candidate <- candidate[pat[i] == vec[candidate + i - 1]]
      candidate
}

patrn <- c(1,2,3,4)
exmpl <- c(3,3,4,2,3,1,2,3,4,8,8,23,1,2,3,4,4,34,4,3,2,1,1,2,3,4)

occur1(patrn, exmpl)
[1]  6 13 23

This was posted some time ago. And seems to be the fastest of the 7
solutions.
https://stat.ethz.ch/pipermail/r-help/2012-February/303756.html

Thanks!

Duncan Murdoch

______________________________________________
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