Creating a for loop based on a condition
Show older comments
Hello everyone, I need to create a for loop which iterates until it is met a specific condition, which in my case is the output being equal to a certain value. I know a while loop would suffice, though I would require a for-loop in order to use the index of the iteration to store the output of said iteration into an array, so that I can have a full accountability of all the results the loop gets.
1 Comment
Dyuman Joshi
on 25 Dec 2023
Moved: Dyuman Joshi
on 26 Dec 2023
Accepted Answer
More Answers (1)
i = 0;
tolerance = 1e-4;
imax = 100;
output = 1;
value = ...;
while abs(output-value) > tolerance && i < imax
i = i + 1;
... % compute something
output = ...;
OutputArray(i) = output;
end
plot(1:i,OutputArray)
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!