title: applying rmultinom in R without iterating over matrix
body: <p>I want to use rmultinom(), combined with a transition matrix, to generate whole number outputs that, when summed, are equal to the original values. However, I can't figure out how to do it without iterating over the matrix. Here is an example:</p>
  <pre><code>a = matrix(runif(16),nrow=4,ncol=4)
  a = apply(a,2,FUN = function(x) x/sum(x))
  
  b = c(5,7,5,9)
  out = c(0,0,0,0) # initialize
  for (i in 1:ncol(a)){
    tmp = rmultinom(1,b[i],a[,i])
    out = tmp + out
  }
  sum(out) == sum(b) ## Should eval to true
  </code></pre>
  <p><code>a</code> represents a transition matrix, with each column summing to 1. <code>b</code> is a starting vector of integers. The loop iterates along the columns to generate a vector in <code>out</code> that sums to the initial numbers in <code>b</code>.  How can I do this without using a loop? The results would be similar to if I multiply <code>a %*% b</code>, but this leaves me with floating point values.</p>