% This file may be used freely for any non-commercial, educational purpose.
% Copyright (c) 2003, Eamonn Keogh, Jessica Lin. All rights reserved.
% 
% This function demonstrates the danger of dimensionality reduction by showing how clouds of 
% points appear to form different numbers of clusters when projected onto different planes. 

function demo_cluster


speed = 0.01; % This varible controls the speed of the animation. WARNING Use low value, less than 0.05

    
data_n = 1000;
    
data1 = ones(data_n, 1)*[0.3 0.2] + randn(data_n, 2)/30;
data2 = ones(data_n, 1)*[0.3 0.5] + randn(data_n,2)/30;
data3 = ones(data_n, 1)*[0.3 0.8] + randn(data_n,2)/30;
data4 = data2;

data4(:,1) = data4(:,1) + 0.4;
data = [data1; data2; data3; data4]

data(:,3) = ones(4000,1)*[0.5] + randn(4000,1)/30;

data1 = [];
data2 = [];
data3 = [];
data4 = [];

hold on;
grid on;
view(135,35);

h = plot3(data(:,1) , data(:,2)  , data(:,3), 'r.' );                           % Plot 3-D cloud of points


set(0,'units','pixels');
screenwidth=get(0,'screensize');
set(gcf,'position',screenwidth*0.9);

set(gca,'xlim',[0 1] );
set(gca,'ylim',[0 1] );
set(gca,'zlim',[0 1] );


title('Assume that you have these clouds of points in 3 dimensions... Click anywhere to continue')
[X,Y] = ginput(1);

title('What will happen if you reduce the dimensionality to just 2 dimensions? Click anywhere to continue')
[X,Y] = ginput(1);


data_size = size(data,1);

plot3(data(:,1) , data(:,2)  , 0 * data(:,3), 'b.' )
title('If you use the X and Y values... you get 4 clusters. Click anywhere to continue');
[X,Y] = ginput(1);

plot3(data(:,1) , 0 * data(:,2) , data(:,3), 'g.' )
title('If you use the X and Z values... you get 2 clusters. Click anywhere to continue');
[X,Y] = ginput(1);

plot3(0 * data(:,1) , data(:,2)  , data(:,3), 'm.' )
title('If you use the Y and Z values... you get 3 clusters. Click anywhere to continue');
[X,Y] = ginput(1);


title(' ');
pause(1) 







% Begin a simple animation that rotates the axis to the XY view, the XZ view and the YZ view, pausing briefly at each.
pause(1) 
for i = 135 : 5 : 180  
    view(i,35 - (abs(i-135)*35/45) )
    pause(speed)    
end
pause(1) 
for i = 180 :-5: 135  
    view(i,35 - (abs(i-135)*35/45) )
    
    pause(speed)    
end

for i = 135 :-5: 90  
    view(i,35 - (abs(i-135)*35/45) )
    
    pause(speed)    
end
pause(1) 
for i = 90 : 5 : 135  
    view(i,35 - (abs(i-135)*35/45) )
    
    pause(speed)    
    
end

for i = 35 : 5 : 90  
    view(135,i)
    
    pause(speed)    
end
pause(1) 
for i = 90 :-5 : 35
    view(135,i)
    
    pause(speed)    
end
title('(c) Eamonn Keogh, Jessica Lin, used with permission')
















