Writing for loop with a intial value given
Show older comments
I have 35 different values of udotreal I have initial value of u I want to write a for loop to get 35 values of u by doing this: New value of u = previous value + corresponding udotreal value. I was trying something like this:
for i = 1:35 u(1,i) = u(1,i) + udotreal(1,i);
end
Please help. Thanks in advanced
Answers (1)
Jos (10584)
on 25 Oct 2017
Start your for-loop at 2, and use the previous value (i-1) to get the new one:
u(1) = ...
for i=2:35
u(i) = u(i-1) + udotreal(i) ; % or udotreal(i-1), I am not sure what you mean
end
3 Comments
Karan Shah
on 27 Oct 2017
Jos (10584)
on 27 Oct 2017
do not create variables named like this! It is the contents of a variable that should be flexible, not its name.
The code above does what you suggest, storing all subsequent values into a single variable, but at subsequent locations, in the variable u. You have to define u(1) though.
Karan Shah
on 27 Oct 2017
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!