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?

 Respuesta aceptada

KSSV
KSSV el 5 de Sept. de 2018

2 votos

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 comentarios

Dion Liu
Dion Liu el 5 de Sept. de 2018
dm = means0-means ;
means0 = means ;
if abs(dm)<=10^-3
Can you explain what this part does please?
KSSV
KSSV el 5 de Sept. de 2018
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.
Dion Liu
Dion Liu el 5 de Sept. de 2018
so how come you did
abs(dm)<=10^-3
instead of
abs(dm)==0
KSSV
KSSV el 5 de Sept. de 2018
It is suggested to use abs(dm)<=10^-3, for comparing floating numbers.
Walter Roberson
Walter Roberson el 5 de Sept. de 2018
You could use a tighter tolerance: it just isn't a good idea to check for bit-for-bit equality.
Dion Liu
Dion Liu el 5 de Sept. de 2018
all right thanks so much
KSSV
KSSV el 5 de Sept. de 2018
Thanks is accepting answer..:)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 5 de Sept. de 2018

Comentada:

el 5 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by