title: r - Fast way to loop through matrix and compute and assign a value to each element?
body: <p>I have a large matrix of probabilities (call it A), N by 806, where N is typically a number in the thousands. </p>
  
  <p>Using this matrix of probabilities, I want to create another matrix (call it B), N by 806, that contains only binary values. The value in B[i,j] is determined by using the corresponding probability in A[i,j] via binomial. The code I am using is below:</p>
  
  <pre><code>diCases &lt;- matrix(0, nrow = numcases, ncol = numdis)
  diConts &lt;- matrix(0, nrow = numconts, ncol = numdis)
  
  for(row in 1:nrow(diCases)) {
      print(paste('Generating disease profile for case', row, '...'))
      for(col in 1:ncol(diCases)) {
          pDis &lt;- Pcases[row, col]
          diCases[row, col] &lt;- rbinom(1, 1, pDis)
      }
  }
  
  for(row in 1:nrow(diConts)) {
      print(paste('Generating disease profile for control', row, '...'))
      for(col in 1:ncol(diConts)) {
          pDis &lt;- Pconts[row, col]
          diConts[row, col] &lt;- rbinom(1, 1, pDis)
      }
  }
  </code></pre>
  
  <p>Basically, I have resorted to using nested for loops, looping through every column in each row and moving on to the next row, assigning a 1 or 0 based on the result of:</p>
  
  <pre><code>rbinom(1, 1, pDis)
  </code></pre>
  
  <p>where pDis is the A[i,j] mentioned in the beginning. As you can imagine, this is pretty slow and is the main bottleneck in my code. This block of code is in a simulation that I had planned to run over and over again, ideally in a short period of time.</p>
  
  <p><strong>Is there a faster way to accomplish this?</strong> I looked into the "apply" functions but couldn't really figure out how to make it work for this particular task.</p>
  
  <p>Thank you all in advance.</p>