How can I save the number of iterations in while loop?

I'm implementing a code to solve linear algebraic system with Gauss-Seidel algorithm. I need to save the number of iterations that are done by the while loop in the iterative process. how can i do?

 Accepted Answer

Set up a counter variable.
Example:
count = 0;
while -CONDITION-
-DO CALCULATIONS-
-UPDATE CONDITION VARIABLE-
count = count + 1;
end

4 Comments

thank you very much for your answer, i've done this and it works but i have to repete the "while" loop for differents values from i=1:n ( like the following code) and i want to store the number of iterations for each i. How can i do?
for i=1:10
count=0
while CONDITION
CALCULATIONS
UPDATE CONDITION VARIABLE
count=count+1
end
end
My pleasure.
That simply requires you to subscript the ‘count’ variable:
for i=1:10
count(i) = 0;
while CONDITION
CALCULATIONS
UPDATE CONDITION VARIABLE
count(i) = count(i)+1
end
end
Now you have values for ‘count’ for all values of ‘i’.
thank you very much, i'm new in matlab and you have been very helpful for me

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations 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!