2

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?

1 Answer 1

3

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

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

?

Sign up to request clarification or add additional context in comments.

1 Comment

I tried that earlier, but I guess I must have made a typo, because that seems to work. Thanks a ton!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.