camdroid
camdroid

Reputation: 524

Matlab Vectorization using Multiple Columns from an Array

I'm trying to vectorize a bit of Matlab code that requires input from two adjacent members of an array. Essentially:

x=1:10;
for i=1:9
    y(i) = x(i)+x(i+1);
end

Is there a way to vectorize this code so that I don't need to use the for loop?

Upvotes: 2

Views: 58

Answers (1)

Oliver Amundsen
Oliver Amundsen

Reputation: 1511

Do I understand this right? Is this what you need?

y = x(1:n-1) + x(2:n);

?

Upvotes: 3

Related Questions