%% Cluster Quasi-Random Data Using Fuzzy C-Means Clustering
% This example shows how FCM clustering works using quasi-random two-dimensional data.
% Load the data set and plot it.

load fcmdata.dat
plot(fcmdata(:,1),fcmdata(:,2),'o')

%
% Next, invoke the command-line function, fcm, to find two clusters in this data set until the objective function is no longer decreasing much at all.

[center,U,objFcn] = fcm(fcmdata,2);

%
% center contains the coordinates of the two cluster centers, U contains the membership grades for each of the data points, and objFcn contains a history of the objective function across the iterations.
% The fcm function is an iteration loop built on top of the following routines:
% initfcm - initializes the problem
% distfcm - performs Euclidean distance calculation
% stepfcm - performs one iteration of clustering
% To view the progress of the clustering, plot the objective function.

figure
plot(objFcn)
title('Objective Function Values')   
xlabel('Iteration Count')
ylabel('Objective Function Value')

%
% Finally, plot the two cluster centers found by the fcm function. The large characters in the plot indicate cluster centers.

maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
figure
line(fcmdata(index1,1), fcmdata(index1,2), 'linestyle',...
                        'none','marker', 'o','color','g')
line(fcmdata(index2,1),fcmdata(index2,2),'linestyle',...
                        'none','marker', 'x','color','r')
hold on
plot(center(1,1),center(1,2),'ko','markersize',15,'LineWidth',2)
plot(center(2,1),center(2,2),'kx','markersize',15,'LineWidth',2)

% Note: Every time you run this example, the fcm function initializes with different initial conditions. This behavior swaps the order in which the cluster centers are computed and plotted.
% Copyright 2015 The MathWorks, Inc.