Cross products of elements of 3D array and matrix columns without loop in R -
i'm working on fishery stock assessment model , want speed removing loop (actually 2 loops of same form). have array, a, dim(a)=[l,l,y], , matrix, m, dim(m)=[l,y]. these used create matrix, mat, dim(mat)=[l,y], calculating matrix products. loop looks like:
for(i in 1:y){ mat[,i]<-(a[,,i]%*%m[,i])[,1]}
can help me out? need speed gain. also, (don't know if it'll create difference but) each a[,,i] matrix lower triangular.
i'm pretty sure give results want. since there no reproducible example, can't absolutely sure. had trace of linear algebra logic see trying accomplish.
library(plyr) # need split array list of 9 matrices b = lapply(alply(a, 3), function(x) (x%*%m)) # perform 9 linear algebra multiplications sapply(1:9, function(i) (b[[i]])[,i]) # extract 9 columns want.
i used next test data:
a = array(rnorm(225), dim = c(5,5,9)) m = matrix(rnorm(45), nrow = 5, ncol = 9)
arrays r performance loops matrix
No comments:
Post a Comment