3

I have an R script that systematically perform changes to a 3x3 matrix by scalar multiplication as follows

R <- matrix(rexp(9, rate=.1), ncol=3);
for (i in 1:360) {
K = i*R;
...
}

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

A[,,i] = i*R;

for all i ranging from 1 to 360?

  • 1
    Interesting question. The resulting array will be quite regular and predictable, in that each "layer" will be related to all other layers by a set of scalars. Is there any way your code might work around needing to create the array, which will be large. You could use `Alyr <- function(i){R*i}`. – CJB Jul 22 '15 at 14:34
  • Within your loop, start with `R` and add `R` to it each time? I don't think the multiplication is slowing you down, though. I'm pretty sure that takes very little time. – Frank Jul 22 '15 at 14:38
  • The modification is simplified here. What I'm actually doing is using [Rodrigues' rotation formula](https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula) in matrix notation to calculate a large number of rotation matrices. I then apply these to a large set of vectors and evaluate the result. So the "real" modification looks something like K = I + sin(theta*pi/180)*T + (1-cos(theta*pi/180))*(T%*%T), where I is the identity matrix and T is the cross-product matrix. So unfortunately the different layers in the array are not THAT predictable... – Christofer Östlin Jul 22 '15 at 14:46

1 Answers1

4

How about some basic multiplication and reshaping

set.seed(15) #for reproducibility
R <- matrix(rexp(9, rate=.1), ncol=3);
R
#           [,1]      [,2]      [,3]
# [1,]  2.042281  1.760375 2.9230182
# [2,] 19.466458  6.628580 0.1818078
# [3,]  2.544348 27.541514 4.1325714

dd <- array(sapply(1:360, `*`, R), dim=c(dim(R), 360))
dd[,,1]
#           [,1]      [,2]      [,3]
# [1,]  2.042281  1.760375 2.9230182
# [2,] 19.466458  6.628580 0.1818078
# [3,]  2.544348 27.541514 4.1325714
dd[,,2]
#           [,1]      [,2]      [,3]
# [1,]  4.084562  3.520749 5.8460364
# [2,] 38.932916 13.257161 0.3636157
# [3,]  5.088697 55.083027 8.2651427
dd[,,10]
#           [,1]      [,2]      [,3]
# [1,]  20.42281  17.60375 29.230182
# [2,] 194.66458  66.28580  1.818078
# [3,]  25.44348 275.41514 41.325714
MrFlick
  • 195,160
  • 17
  • 277
  • 295