Multiplying one matrix with a set of scalars <p>I have an R script that systematically perform changes to a 3x3 matrix by scalar multiplication as follows</p> <pre><code>R &lt;- matrix(rexp(9, rate=.1), ncol=3); for (i in 1:360) { K = i*R; ... } </code></pre> <p>and use the modified matrix for further calculations within the loop. However, the loop itself is nested inside two other for loops, making the script very slow. So my question is, how can I vectorize this innermost loop such that the result instead is a three-dimensional array A of size 3x3x360 where</p> <pre><code>A[,,i] = i*R; </code></pre> <p>for all i ranging from 1 to 360?</p>