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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With