title: Getting rows of a subcolumns of a matrix which coincide with a series of vectors, without using apply
body: <p>This is a bit of a challenging one so I did my best to be reproducible/follow guidelines/etc. </p>
  
  <p>This is related to my earlier question <a href="https://stackoverflow.com/questions/32654938/getting-rows-of-a-matrix-which-coincide-with-a-series-of-vectors-without-using">here</a>, but now I want to add one more dimension.  The solution needs to be VERY fast, so no looping, apply, or slow merges if possible. </p>
  
  <p>Consider the below:</p>
  
  <pre><code>set.seed(1)
  
      M = matrix(rpois(50,5),5,5)
  
  
  
          v1 = c(4  ,  8  ,  3 ,   5 ,   9)       
          v2 = c(5  ,  6  ,  6 ,  11  ,  6)
          v3 = c( 5  ,  6 ,   6 ,  11  ,  6)
          v4=  c(8, 6,  4, 4, 3)
          v5 =  c(4  ,  8  ,  3 ,   5  ,  9)
          v6=  c(8  ,  6  ,  4  ,  4 ,   3)
          v7 = c( 3 ,   2  ,  7   , 7 ,   4)
          v8=  c(3  ,  2   , 7   , 7  ,  4)
  
  row1 = c(v1,v2)
  row2 = c(v3,v4)
  
  row3 = c(v5,v6)
  
  row4 = c(v7,v8)
  
  Vmat = rbind(row1,row2,row3,row4)
  
  
       M
       [,1] [,2] [,3] [,4] [,5]
  [1,]    4    8    3    5    9
  [2,]    4    9    3    6    3
  [3,]    5    6    6   11    6
  [4,]    8    6    4    4    3
  [5,]    3    2    7    7    4
  
  
  
  
   Vmat
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
  row1    4    8    3    5    9    5    6    6   11     6
  row2    5    6    6   11    6    8    6    4    4     3
  row3    4    8    3    5    9    8    6    4    4     3
  row4    3    2    7    7    4    3    2    7    7     4
  </code></pre>
  
  <p>Each row of Vmat is composed of two rows of M stacked side by side.  Hence...</p>
  
  <p>Consider mentally Vmat into 2 matrices (in my problem, it is many more than 2, up to 500,000 across) between columns 5 and 6.</p>
  
  <p>For each submatrix of Vmat, I want to say where each row vector corresponds to the row in M.  </p>
  
  <pre><code>The output should thus be be...
  
  
  
        [,1] [,2]
  [1,]    1    3
  [2,]    3    4
  [3,]    1    4
  [4,]    5    5
  </code></pre>
  
  <p>I'm thinking maybe stacking the Vmat matrix like in <a href="https://stackoverflow.com/questions/32793326/stack-every-n-columns-of-a-matrix-without-apply-in-r">this question</a> could be a first pass, then doing the row lookups, then reshaping.</p>