Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickly compute `dot(a(n:end), b(1:end-n))`

Suppose we have two, one dimensional arrays of values a and b which both have length N. I want to create a new array c such that c(n)=dot(a(n:N), b(1:N-n+1)) I can of course do this using a simple loop:

for n=1:N
    c(n)=dot(a(n:N), b(1:N-n+1));
end

but given that this is such a simple operation which resembles a convolution I was wondering if there isn't a more efficient method to do this (using Matlab).

like image 225
Darkwizie Avatar asked Dec 17 '22 18:12

Darkwizie


1 Answers

A solution using 1D convolution conv:

out = conv(a, flip(b));
c = out(ceil(numel(out)/2):end);

In conv the first vector is multiplied by the reversed version of the second vector so we need to compute the convolution of a and the flipped b and trim the unnecessary part.

like image 180
rahnema1 Avatar answered Jan 02 '23 15:01

rahnema1