First time posting a question here! Will happily take any advice||criticism I can get.
We have two vectors: v1 and v2. Assume length(v1) >> length(v2). I move a window of size length(v2) along the vector v1. At each lag index, from the windowed portion of v1 I subtract v2. I then sum the terms of the resulting vector and return this sum for every lag index along the length of vector v1. For simplicity, let’s ignore the edge cases.
I’ve done this with a for loop, but the length of my vectors is on the order of 10^9 [and larger], and even though the calculation is simple, it appears to take a long time to just iterate through the whole thing.
Any ideas? I suspect there is a function that does something like this, but I have had no luck finding it.
If you’ll allow me to reframe the question, you basically want a windowed sum of one vector. You’re then subtracting from this the sum of some other vector.
My solution would be to use the cumulative sum function in matlab (
cumsum) as follows:This assumes that
v1andv2are row-vectors. It padsv1with zeros on the right, takes the cumulative sum, then subtracts the cumulative sum with the zeros padded to the left. This results in a vector such thatwindowSum(i)is the sum of thelength(v2)elements ofv1preceeding indexi. Depending on how you want the edges handled, what you want the length of the vector to be, etc., this should solve your problem.