Writing for loop with a intial value given
Afficher commentaires plus anciens
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
Réponses (1)
Jos (10584)
le 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 commentaires
Karan Shah
le 27 Oct 2017
Jos (10584)
le 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
le 27 Oct 2017
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!