Writing for loop with a intial value given
Ältere Kommentare anzeigen
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
Antworten (1)
Jos (10584)
am 25 Okt. 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 Kommentare
Karan Shah
am 27 Okt. 2017
Jos (10584)
am 27 Okt. 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
am 27 Okt. 2017
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!