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?

 채택된 답변

KSSV
KSSV 2018년 9월 5일

2 개 추천

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

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 9월 5일

댓글:

2018년 9월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by