Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, here is your code first. We run this so that we have a copy of the "true" matrix, the one we hope to reproduce, faster.</p> <pre><code>#### Original Code (aka Gold Standard) #### M = matrix(1:48, ncol = 3) k = nrow(M)/2 M[-(1:k), ] = 0 nLeft = nrow(M) - k nextRow = k + 1 idxList = matrix(1:k, ncol = 2) for ( i in 1 : (nLeft / 2)) { row1 = M[idxList[i, 1],] row2 = M[idxList[i, 2],] results = matrix(c(2*row1, 3*row2), ncol = 2) M[nextRow, ] = results[, 1] nextRow = nextRow + 1 M[nextRow, ] = results[, 2] nextRow = nextRow + 1 } </code></pre> <p>Now here is the vectorized code. The basic idea is if you have 4 rows you are processing. Rather than passing them as vectors one at a time, do it at once. That is:</p> <pre><code>(1:3) * 2 (1:3) * 2 (1:3) * 2 (1:3) * 2 </code></pre> <p>is the same (but slower) as:</p> <pre><code>c(1:3, 1:3, 1:3, 1:3) * 2 </code></pre> <p>So first, we will use your same setup code, then create the rows to be processed as two long vectors (where all 4 original rows are just strung together as in my simple example above). Then, we take those results, and transform them into matrices with the appropriate dimensions. The last trick is to assign the results back in in just two steps. You can assign to multiple rows of a matrix at once, so we use <code>seq()</code> to get odd and even numbers so assign the first and second column of the results to, respectively.</p> <pre><code>#### Vectorized Code (testing) #### M2 = matrix(1:48, ncol = 3) k2 = nrow(M2)/2 M2[-(1:k2), ] = 0 nLeft2 = nrow(M2) - k2 nextRow2 = k2 + 1 idxList2 = matrix(1:k2, ncol = 2) ## create two long vectors of all rows to be processed row12 &lt;- as.vector(t(M2[idxList2[, 1],])) row22 &lt;- as.vector(t(M2[idxList2[, 2],])) ## get all results results2 = matrix(c(2*row12, 3*row22), ncol = 2) ## add results back M2[seq(nextRow2, nextRow2 + nLeft2-1, by = 2), ] &lt;- matrix(results2[,1], nLeft2/2, byrow=TRUE) M2[seq(nextRow2+1, nextRow2 + nLeft2, by = 2), ] &lt;- matrix(results2[,2], nLeft2/2, byrow=TRUE) ## check that vectorized code matches your examples all.equal(M, M2) </code></pre> <p>Which on my machine gives:</p> <pre><code>&gt; all.equal(M, M2) [1] TRUE </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload