MATLAB Answers

Tétef
0

Vectorization of product of flipped vectors

Asked by Tétef
on 22 Feb 2013

I'm looking for how to vectorize this loop :

r=zeros(1,length(r1));
for i=1:length(r1);
r(i)=sum(r1(1:i).*r2(i:-1:1));
end

r1 and r2 are two vectors of the same size.

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by Teja Muppirala
on 22 Feb 2013
 Accepted answer

Faster for long vectors, slower for short ones:

r = conv(r1,r2);
r = r(1:numel(r1));

  1 Comment

Tétef
on 22 Feb 2013

thx !


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!