title: How to count the number of iterations
body: <p>I'm working with k-means on MATLAB. To process the valid cluster, it needs to do a looping until the cluster position doesn't change any more. The looping will show the iterations process.</p>
  
  <p>I want to count how many looping/iteration happens on that clustering process. Here is the snippet of looping/iteration processing part:</p>
  
  <pre><code>while 1,
      d=DistMatrix3(data,c);  %// calculate the distance
      [z,g]=min(d,[],2);      %// set the matrix g group
  
      if g==temp,             %// if the iteration does not change anymore
          break;              %// stop the iteration
      else
          temp=g;             %// copy the matrix to the temporary variable
      end
      for i=1:k
          f=find(g==i);
          if f                %// calculate the new centroid
              c(i,:)=mean(data(find(g==i),:),1);
          end
      end
  end
  </code></pre>
  
  <p>All I know that I have to do is define the iteration variable, and then write the calculation part. But, where do I have to define the variable? And how?</p>
  
  <p>All the answers will be so much appreciated.</p>
  
  <p>Thank you.</p>