close all, clear all, clc

% Create data; 100 points in each of three groups.
set1x = normrnd(3,1,100,1);
set1y = normrnd(7,1,100,1);
set2x = normrnd(-3,1,100,1);
set2y = normrnd(-2,1,100,1);
set3x = normrnd(6,1,100,1);
set3y = normrnd(-2,1,100,1);
data = [set1x set1y; set2x set2y; set3x set3y];

% Randomize order of points
perm = randperm(300);
data = data(perm,:);

% Plot original data
scatter(data(:,1),data(:,2),'+')
title('Before clustering')

% Plot clustered data
T = clusterdata(data,3);
figure
gscatter(data(:,1),data(:,2),T,[],'+*^')
title('After hierarchical clustering')

% Try K-means clustering instead.
T = kmeans(data,3,'replicates',5);
figure
gscatter(data(:,1),data(:,2),T,[],'+*^')
title('After K-means clustering')