Researched a little bit and for some reason it's not converging. The sum of distances is decreasing, but points keep on getting moved around in phase 2 up to 1000 iterations.
To be more precise, I've made 2 changes:
In the distfun function I changed:
D(:,i) = (X(:,1) - C(i,1)).^2;
for j = 2:p
D(:,i) = D(:,i) + (X(:,j) - C(i,j)).^2;
end
to
D(:,i) = sqrt((X(:,1) - C(i,1)).^2 + (X(:,2) - C(i,2)).^2)+sqrt((X(:,3) - C(i,3)).^2 + (X(:,4) - C(i,4)).^2);
and at the beginning of phase 2 in calculating distances to each cluster from each point I changed:
Del(:,i) = (m(i) ./ (m(i) + sgn)) .* sum((bsxfun(@minus, X, C(i,:))).^2, 2);
to:
Del(:,i) = (m(i) ./ (m(i) + sgn)) .* (sqrt(sum((bsxfun(@minus, X(:,1:2), C(i,1:2))).^2,2))+sqrt(sum((bsxfun(@minus, X(:,3:4), C(i,3:4))).^2,2)));
my points have 4 dimensions, and if one point is (x1,y1,w1,z1) and another (x2,y2,w2,z2), then the distance that I want to use is sqrt((x1-x2)^2+(y1-y2)^2) + sqrt((w1-w2)^2+(z1-z2)^2)
Would I need to change the K-means function somewhere else for this to work?
Would really appreciate the help of anyone's familiar with the builtin k-means function, as I am stuck.