Finding pattern in a matrix in R

前端 未结 7 760
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 04:40

I have a 8 x n matrix, for instance

set.seed(12345)
m <- matrix(sample(1:50, 800, replace=T), ncol=8)
head(m)

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8         


        
相关标签:
7条回答
  • 2020-12-30 05:33

    Here's one way using sapply:

    which(sapply(seq(nrow(m)-2),
                 function(x)
                   isTRUE(37 %in% m[x,] & 
                          which(10 == m[x+1,]) < which(29 == m[x+1,]) & 
                          42 %in% m[x+2,])))
    

    The result contains all row number where the sequence starts:

    [1] 57
    
    0 讨论(0)
提交回复
热议问题