function v = wkmeans_variance ( dim, n, p, k, c, ptoc )

%% WKMEANS_VARIANCE computes the variance of the weighted K-means clustering.
%
  pmc(1:dim,1:n) = p(1:dim,1:n) - c(1:dim,ptoc(1:n));

  v = 0.0;
  for j = 1 : k
    index = find ( ptoc(1:n) == j );
    wpmc(index) = w(index) .* ( norm ( pmc(1:dim,index) ) )^2;
    var(j) = sum ( wpmc(index) ) / sum ( w(index) );
    v = v + var(j);
  end

  return
end
