any way to avoid this loop?

1 view (last 30 days)
Chad Greene
Chad Greene on 6 Feb 2014
Commented: Chad Greene on 6 Feb 2014
I'm familiar with the benefits of vectorizing code, but is there a way to avoid a loop when the solution to each value in an array depends on the value before it? A minimal working example of a loop I'd like to avoid:
x = ones(1,1000);
for n = 2:length(x)
x(n) = 2*x(n-1) - 10;
end
Ideas for a generalized solution? Or perhaps there's simply a faster technique I should learn about?

Accepted Answer

per isakson
per isakson on 6 Feb 2014
  1 Comment
Chad Greene
Chad Greene on 6 Feb 2014
Brilliant! Exactly the solution I was hoping for. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!