Reputation: 1769
I have two arrays of n elements (arrays p and q):
p1 p2 p3 ...
q1 q2 q3 ...
At first calculate, I should calculate
p1*q1+p2*q2
At second:
p1*q1+p2*q2+p3*q3+p4*q4
At third:
p1*q1+p2*q2+p3*q3+p4*q4+p5*q5+p6*q6
and so on, for k times. These can be view as products between row vector and column vector, so I tried to use the matrix multiplication:
Mat<-t(replicate(k, p))
Mat[col(Mat)>2*row(Mat)] <- 0
Res<-Mat%*%q
Nevertheless, the size of matrix Mat must be too large, because I obtain
Error: cannot allocate vector of size 27.7 Mb
Probably I could solve that problem using bigmemory
package, as suggested by several posts on this forum, but I wonder if there is a way to simplify my idea. For example, I observe that with matrix Mat
we allocate several entries which we don't use (the 0s).
Upvotes: 1
Views: 205
Reputation: 25435
I think this does what you want:
set.seed(1)
p = sample(1:5,100,replace=T)
q = sample(1:5,100,replace=T)
# my initial idea was the following, but that contains a lot of redundant calculations...
sapply(1:length(p), function(x) {sum(head(q,x)*head(p,x))})
# a lot simpler:
cumsum(p*q)
first element is p[1]*q[1]
, second element is p[1]*q[1] + p[2]*q[2]
, etc.
Hope this helps!
Upvotes: 2