Writing for loop with a intial value given
Mostrar comentarios más antiguos
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
Respuestas (1)
Jos (10584)
el 25 de Oct. de 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 comentarios
Karan Shah
el 27 de Oct. de 2017
Jos (10584)
el 27 de Oct. de 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
el 27 de Oct. de 2017
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!