how to check convergence

I want to stop this while loop if the means array does not change anymore, so basically when convergence occurs.
while i <= maxIterations
k = size(seedMeans,1);
means = UpdateMeans(A,k, clusters);
i = i+1;
How should I do this?

 Accepted Answer

YOu have to proceed something like this:
means0 = 0 ;
while i <= maxIterations
k = size(seedMeans,1);
means = UpdateMeans(A,k, clusters);
dm = means0-means ;
means0 = means ;
if abs(dm)<=10^-3
break
end
i = i+1;
end

7 Comments

dm = means0-means ;
means0 = means ;
if abs(dm)<=10^-3
Can you explain what this part does please?
means0 is previous iteration value, mean is present iteration value. If both the values are same, the difference dm will be zero. So, it exits from the loop.
so how come you did
abs(dm)<=10^-3
instead of
abs(dm)==0
It is suggested to use abs(dm)<=10^-3, for comparing floating numbers.
You could use a tighter tolerance: it just isn't a good idea to check for bit-for-bit equality.
all right thanks so much
Thanks is accepting answer..:)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 5 Sep 2018

Commented:

on 5 Sep 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!