title: Replicate rows of a matrix in R
body: <p>Suppose I have a matrix <code>m</code> and a positive integer vector <code>v</code>, what I want to do is get a new matrix <code>m_new</code> and each row of <code>m</code> (say <code>m[i, ]</code>) are replicated by <code>v[i]</code> times in <code>m_new</code>. For example:</p>
  
  <pre><code>m = matrix(1:6, nrow = 3)
  ##      [,1] [,2]
  ## [1,]    1    4
  ## [2,]    2    5
  ## [3,]    3    6
  v = c(3, 1, 2)
  </code></pre>
  
  <p>And <code>m_new</code> should be:</p>
  
  <pre><code>     [,1] [,2]
  [1,]    1    4  # m[1, ] is replicated by
  [2,]    1    4  # v[1] = 3
  [3,]    1    4  # times
  [4,]    2    5   
  [5,]    3    6  
  [6,]    3    6  
  </code></pre>
  
  <p>A <code>for</code> loop will make it for the small case:</p>
  
  <pre><code>m_new = matrix(0, sum(v), ncol(m))
  k = 1
  for(i in 1:nrow(m)){
      for(j in k:(k+v[i]-1)){
          m_new[j, ] = m[i, ]
      }
      k = k + v[i]
  }
  </code></pre>
  
  <p>, but the row number of <code>m</code> in real world is usually big. Is there any effient way to do this?</p>