Speeding up nested for-loop analysis code
Try the following:

mat0 &lt;- tep[, c(TRUE, FALSE, FALSE)]
mat1 &lt;- tep[, c(FALSE, TRUE, FALSE)]
mat2 &lt;- tep[, c(FALSE, FALSE, TRUE)]

denominator &lt;- mat0 + mat1 + mat2
numerator   &lt;- mat0 * (mat - 1) * (mat - 2) / (0 - 1) / (0 - 2) +
               mat1 * (mat - 0) * (mat - 2) / (1 - 0) / (1 - 2) +
               mat2 * (mat - 0) * (mat - 1) / (2 - 0) / (2 - 1)

A &lt;- numerator / denominator