[R] Calculating subsets of row pairs using somthing faster than a for loop.

Bernzweig, Bruce (Consultant) bbernzwe at bear.com
Tue Jul 24 19:37:41 CEST 2007

Hi all,




 - I have two matrices each w/ 4 rows and 20 columns.


mat1 <- matrix(sample(1:500,80), ncol = 20, 

            dimnames=list(paste("mat1row", 1:4, sep=""), 

            paste("mat1col", 1:20, sep="")))


mat2 <- matrix(sample(501:1000,80), ncol = 20, 

            dimnames=list(paste("mat2row", 1:4, sep=""), 

            paste("mat2col", 1:20, sep="")))


 - Each column represents a value in a time series.


Q: What do I want:


   Calculate moving average correlations for each row x row pair:


   For each row x row pair I want 10 values representing moving average

   correlations for 10 sets of time-values:


   cor(mat1[1,1:10], mat2[1,1:10])

   cor(mat1[1,2:11], mat2[1,2:11])


   cor(mat1[1,11:20], mat2[1,11:20])

   cor(mat1[1,1:10], mat2[2,1:10])


   cor(mat1[4,11:20], mat2[4,11:20])


   Result would be a 16 (rows) x 10 (col) matrix matMA


      ma1, ma2, ..., ma10 for (mat1 row1) x (mat2 row1)

      ma1, ma2, ..., ma10 for (mat1 row1) x (mat2 row2)


      ma1, ma2, ..., ma10 for (mat1 row4) x (mat2 row3)

      ma1, ma2, ..., ma10 for (mat1 row4) x (mat2 row4) 


   I would like to be able to do this without using a for loop

   due to the slowness of that method.


   Is it possible to iterate through subsets w/o using a for loop?




- Bruce



-------------- next part --------------

Please be aware that, notwithstanding the fact that the pers...{{dropped}}

More information about the R-help mailing list