[matlab] 14.DBSCAN 聚类

%x=[randn(30,2)*.4;randn(40,2)*.5+ones(40,1)*[4 4]];  测试 数据 或者 可以另外指定
X=[56.70466067,56.70466067,56.70466067,56.70466067,56.70466067,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,21.00466067,22.33256629,22.33256629,23.6604719,24.98837752,24.98837752,24.98837752,26.31628314,26.31628314,26.31628314,26.31628314,27.64418876,27.64418876,27.64418876,27.64418876,27.64418876,28.97209438,28.97209438,28.97209438,28.97209438,28.97209438,28.97209438,28.97209438,30.3,30.3,30.3,30.3,31.62790562,31.62790562,31.62790562,32.95581124,32.95581124,32.95581124,34.28371686,34.28371686,34.28371686,35.61162248,35.61162248,35.61162248,36.9395281,89.10466067,89.10466067,90.43256629,90.43256629,93.08837752,93.08837752,93.08837752,93.08837752,94.41628314,94.41628314,94.41628314,94.41628314,94.41628314,94.41628314,94.41628314,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,98.4,98.4,98.4,98.4,98.4,99.72790562,99.72790562,99.72790562,99.72790562,101.0558112,101.0558112,101.0558112,101.0558112,102.3837169,102.3837169,102.3837169,103.7116225,103.7116225,103.7116225,103.7116225,105.0395281,105.0395281,105.0395281,106.3674337,106.3674337,64.40466067,64.40466067,65.73256629,65.73256629,65.73256629,65.73256629,65.73256629,67.0604719,67.0604719,67.0604719,67.0604719,68.38837752,68.38837752,68.38837752,68.38837752,69.71628314,69.71628314,69.71628314,69.71628314,71.04418876,71.04418876,71.04418876,71.04418876,71.04418876,72.37209438,72.37209438,72.37209438,72.37209438,72.37209438,73.7,73.7,73.7,73.7,75.02790562,75.02790562,75.02790562,75.02790562,76.35581124,76.35581124,76.35581124,77.68371686,77.68371686,48.60466067,49.93256629,49.93256629,49.93256629,49.93256629,49.93256629,49.93256629,49.93256629,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,57.9,57.9,57.9,57.9,57.9,57.9,59.22790562,59.22790562,59.22790562,59.22790562,60.55581124,60.55581124,60.55581124,61.88371686,61.88371686,61.88371686,63.21162248,63.21162248];
X=X(:);
Y=[82.04418876,83.37209438,84.7,86.02790562,87.35581124,80.71628314,82.04418876,83.37209438,84.7,86.02790562,87.35581124,88.68371686,90.01162248,78.0604719,79.38837752,80.71628314,82.04418876,83.37209438,84.7,88.68371686,90.01162248,91.3395281,76.73256629,78.0604719,79.38837752,80.71628314,82.04418876,83.37209438,87.35581124,88.68371686,91.12790562,92.45581124,93.78371686,93.78371686,91.12790562,93.78371686,95.11162248,92.45581124,93.78371686,95.11162248,96.4395281,91.12790562,92.45581124,95.11162248,96.4395281,97.76743371,80.50466067,81.83256629,91.12790562,95.11162248,96.4395281,97.76743371,99.09533933,80.50466067,96.4395281,97.76743371,99.09533933,96.4395281,97.76743371,99.09533933,95.11162248,97.76743371,99.09533933,95.11162248,96.4395281,97.76743371,95.11162248,96.4395281,97.76743371,96.4395281,78.02790562,79.35581124,78.02790562,79.35581124,71.38837752,82.01162248,83.3395281,84.66743371,72.71628314,74.04418876,75.37209438,76.7,82.01162248,83.3395281,84.66743371,74.04418876,75.37209438,76.7,78.02790562,79.35581124,83.3395281,84.66743371,85.99533933,74.04418876,75.37209438,76.7,78.02790562,79.35581124,80.68371686,84.66743371,85.99533933,74.04418876,78.02790562,79.35581124,80.68371686,82.01162248,74.04418876,80.68371686,82.01162248,83.3395281,80.68371686,82.01162248,83.3395281,84.66743371,82.01162248,83.3395281,84.66743371,80.68371686,82.01162248,83.3395281,84.66743371,80.68371686,82.01162248,83.3395281,79.35581124,80.68371686,58.34418876,59.67209438,55.68837752,57.01628314,58.34418876,59.67209438,66.31162248,54.3604719,55.68837752,57.01628314,67.6395281,53.03256629,54.3604719,55.68837752,67.6395281,53.03256629,54.3604719,55.68837752,57.01628314,53.03256629,54.3604719,55.68837752,57.01628314,58.34418876,53.03256629,54.3604719,55.68837752,57.01628314,58.34418876,51.70466067,53.03256629,57.01628314,58.34418876,51.70466067,53.03256629,58.34418876,70.29533933,51.70466067,53.03256629,58.34418876,53.03256629,57.01628314,46.27209438,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,40.9604719,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,51.58371686,52.91162248,40.9604719,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,51.58371686,52.91162248,54.2395281,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,51.58371686,52.91162248,54.2395281,55.56743371,42.28837752,43.61628314,44.94418876,46.27209438,47.6,51.58371686,52.91162248,54.2395281,55.56743371,40.9604719,42.28837752,46.27209438,47.6,51.58371686,52.91162248,54.2395281,55.56743371,56.89533933,40.9604719,46.27209438,47.6,54.2395281,55.56743371,56.89533933,39.63256629,40.9604719,54.2395281,55.56743371,39.63256629,40.9604719,55.56743371,52.91162248,54.2395281,55.56743371,54.2395281,55.56743371];
Y=Y(:);

point=[X,Y];

%  x=[randn(3 , 2)*.4;  randn(4,2)*.5 + ones(4,1)*[4 4]];

%[class,type]=dbscan(x,5,[]);
%[class,type]=dbscan(x,7,[]);
%[class,type]=dbscan(x,10,[]);
%[class,type]=dbscan(x,10,2);
[class,type]=dbscan(point,3,[]);
color_cluster(point,class);
main
% 要可视化聚类结果很简单,实现一个可视化2D数据聚类结果的函数:

% -------------
% 输入:
% data:数据集
% class:每个数据点的分类,-1表示噪点,分类从1开始
% -------------

function color_cluster(data, class)
color_theme = {'ro','rv','bx','ks','r+','go','k*','r+','gv','bs','k+', ...
    'gv','gx','bo','kx','rs','gs','b+','ko',...
    'rx','g+','bv','kv'};
non_class_color = 'm*'; %噪点
[m, n] = size(data);
subplot(1,2,1);
%plot(data(:,1),data(:,2),'ko');
plot(data(:,1),data(:,2),'ko');
title('raw data');
subplot(1,2,2);

for x = 1:m
    if class(x) == -1
        plot(data(x,1),data(x,2) ,non_class_color)
    else
        plot(data(x,1),data(x,2) ,cell2mat(color_theme(class(x))))
    end
    hold on;
end
title('result');
% % 我们测试一下:
% % 
% % x=[randn(30,2)*.4;randn(40,2)*.5+ones(40,1)*[4 4]];
% % [class,type]=dbscan(x,5,[]);
% % color_cluster(x,class);
color_cluster.m
function [D]=dist(i,x)

% function: [D]=dist(i,x)
%
% Aim: 
% Calculates the Euclidean distances between the i-th object and all objects in x     
%                                    
% Input: 
% i - an object (1,n)
% x - data matrix (m,n); m-objects, n-variables        
%                                                                 
% Output: 
% D - Euclidean distance (m,1)



[m,n]=size(x);
D=sqrt(sum((((ones(m,1)*i)-x).^2)'));

if n==1
   D=abs((ones(m,1)*i-x))';
end
dist.m
function [Eps]=epsilon(x,k)
% Aim: 
% Analytical way of estimating neighborhood radius for DBSCAN
%
% Input: 
% x - data matrix (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object
% (minimal number of objects considered as a cluster)

[m,n]=size(x);

Eps=((prod(max(x)-min(x))*k*gamma(.5*n+1))/(m*sqrt(pi.^n))).^(1/n);
epsilon.m
% % 一、基于密度的聚类算法的概述
% %     最近在Science上的一篇基于密度的聚类算法《Clustering by fast search and find of density peaks》引起了大家的关注(在我的博文“论文中的机器学习算法——基于密度峰值的聚类算法”中也进行了中文的描述)。于是我就想了解下基于密度的聚类算法,熟悉下基于密度的聚类算法与基于距离的聚类算法,如K-Means算法之间的区别。
% %     基于密度的聚类算法主要的目标是寻找被低密度区域分离的高密度区域。与基于距离的聚类算法不同的是,基于距离的聚类算法的聚类结果是球状的簇,而基于密度的聚类算法可以发现任意形状的聚类,这对于带有噪音点的数据起着重要的作用。
% % 二、DBSCAN算法的原理
% % 1、基本概念
% %     DBSCAN(Density-Based Spatial Clustering of Application with Noise)是一种典型的基于密度的聚类算法,在DBSCAN算法中将数据点分为一下三类:
% % 核心点。在半径Eps内含有超过MinPts数目的点
% % 边界点。在半径Eps内点的数量小于MinPts,但是落在核心点的邻域内
% % 噪音点。既不是核心点也不是边界点的点
% % 在这里有两个量,一个是半径Eps,另一个是指定的数目MinPts。
% %     一些其他的概念
% % Eps邻域。简单来讲就是与点的距离小于等于Eps的所有的点的集合,可以表示为。
% % 直接密度可达。如果在核心对象的Eps邻域内,则称对象从对象出发是直接密度可达的。
% % 密度可达。对于对象链:,是从关于Eps和MinPts直接密度可达的,则对象是从对象关于Eps和MinPts密度可达的。
% % 2、算法流程

% -------------------------------------------------------------------------
% Function: [class,type]=dbscan(x,k,Eps)
% -------------------------------------------------------------------------
% Aim: 
% Clustering the data with Density-Based Scan Algorithm with Noise (DBSCAN)
% -------------------------------------------------------------------------
% Input: 
% x - data set (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object 
% (minimal number of objects considered as a cluster)
% Eps - neighborhood radius, if not known avoid this parameter or put []
% -------------------------------------------------------------------------
% Output: 
% class - vector specifying assignment of the i-th object to certain 
% cluster (m,1)
% type - vector specifying type of the i-th object 
% (core: 1, border: 0, outlier: -1)
% -------------------------------------------------------------------------
% Example of use:
% x=[randn(30,2)*.4;randn(40,2)*.5+ones(40,1)*[4 4]];
% [class,type]=dbscan(x,5,[])
% clusteringfigs('Dbscan',x,[1 2],class,type)
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% Input: 
% x - data set (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object 
% (minimal number of objects considered as a cluster)
% Eps - neighborhood radius, if not known avoid this parameter or put []
% -------------------------------------------------------------------------


function [class,type]=dbscan(x,k,Eps)

[m,n]=size(x);  %得到数据的大小

if nargin<3 | isempty(Eps)
   [Eps]=epsilon(x,k);                %  ??
end

x=[[1:m]' x];
[m,n]=size(x);      %重新计算数据集的大小
type=zeros(1,m);  %用于区分核心点1,边界点0和噪音点-1
no=1;                         %用于标记类
touched=zeros(m,1);  %用于判断该点是否处理过,0表示未处理过

%% 对每一个点进行处理
for i=1:m
    %找到未处理的点
    if touched(i)==0;
       ob=x(i,:);
       D=dist(ob(2:n),x(:,2:n));  %取得第i个点到其他所有点的距离
       ind=find(D<=Eps);            %找到半径Eps内的所有点
    
       %% 区分点的类型
       
       %边界点
       if length(ind)>1 & length(ind)<k+1       
          type(i)=0;
          class(i)=0;
       end
       
       %噪音点
       if length(ind)==1
          type(i)=-1;
          class(i)=-1;  
          touched(i)=1;
       end

       %核心点(此处是关键步骤)
       if length(ind)>=k+1; 
          type(i)=1;
          class(ind)=ones(length(ind),1)*max(no);
          
          % 判断核心点是否密度可达
          while ~isempty(ind)
                ob=x(ind(1),:);
                touched(ind(1))=1;
                ind(1)=[];
                D=dist(ob(2:n),x(:,2:n));  %找到与ind(1)之间的距离
                i1=find(D<=Eps);
     
                if length(i1)>1   %处理非噪音点
                   class(i1)=no;
                   if length(i1)>=k+1;
                      type(ob(1))=1;
                   else
                      type(ob(1))=0;
                   end

                   for i=1:length(i1)
                       if touched(i1(i))==0
                          touched(i1(i))=1;
                          ind=[ind i1(i)];   
                          class(i1(i))=no;
                       end                    
                   end
                end
          end
          no=no+1; 
       end
   end
end
% 最后处理所有未分类的点为噪音点
i1=find(class==0);
class(i1)=-1;
type(i1)=-1;
dbscan.m

 只标记分类 不绘图

%只筛选标记 不作绘图
X=[56.70466067,56.70466067,56.70466067,56.70466067,56.70466067,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,58.03256629,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,59.3604719,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,60.68837752,21.00466067,22.33256629,22.33256629,23.6604719,24.98837752,24.98837752,24.98837752,26.31628314,26.31628314,26.31628314,26.31628314,27.64418876,27.64418876,27.64418876,27.64418876,27.64418876,28.97209438,28.97209438,28.97209438,28.97209438,28.97209438,28.97209438,28.97209438,30.3,30.3,30.3,30.3,31.62790562,31.62790562,31.62790562,32.95581124,32.95581124,32.95581124,34.28371686,34.28371686,34.28371686,35.61162248,35.61162248,35.61162248,36.9395281,89.10466067,89.10466067,90.43256629,90.43256629,93.08837752,93.08837752,93.08837752,93.08837752,94.41628314,94.41628314,94.41628314,94.41628314,94.41628314,94.41628314,94.41628314,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,95.74418876,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,97.07209438,98.4,98.4,98.4,98.4,98.4,99.72790562,99.72790562,99.72790562,99.72790562,101.0558112,101.0558112,101.0558112,101.0558112,102.3837169,102.3837169,102.3837169,103.7116225,103.7116225,103.7116225,103.7116225,105.0395281,105.0395281,105.0395281,106.3674337,106.3674337,64.40466067,64.40466067,65.73256629,65.73256629,65.73256629,65.73256629,65.73256629,67.0604719,67.0604719,67.0604719,67.0604719,68.38837752,68.38837752,68.38837752,68.38837752,69.71628314,69.71628314,69.71628314,69.71628314,71.04418876,71.04418876,71.04418876,71.04418876,71.04418876,72.37209438,72.37209438,72.37209438,72.37209438,72.37209438,73.7,73.7,73.7,73.7,75.02790562,75.02790562,75.02790562,75.02790562,76.35581124,76.35581124,76.35581124,77.68371686,77.68371686,48.60466067,49.93256629,49.93256629,49.93256629,49.93256629,49.93256629,49.93256629,49.93256629,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,51.2604719,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,52.58837752,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,53.91628314,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,55.24418876,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,56.57209438,57.9,57.9,57.9,57.9,57.9,57.9,59.22790562,59.22790562,59.22790562,59.22790562,60.55581124,60.55581124,60.55581124,61.88371686,61.88371686,61.88371686,63.21162248,63.21162248];
X=X(:);
Y=[82.04418876,83.37209438,84.7,86.02790562,87.35581124,80.71628314,82.04418876,83.37209438,84.7,86.02790562,87.35581124,88.68371686,90.01162248,78.0604719,79.38837752,80.71628314,82.04418876,83.37209438,84.7,88.68371686,90.01162248,91.3395281,76.73256629,78.0604719,79.38837752,80.71628314,82.04418876,83.37209438,87.35581124,88.68371686,91.12790562,92.45581124,93.78371686,93.78371686,91.12790562,93.78371686,95.11162248,92.45581124,93.78371686,95.11162248,96.4395281,91.12790562,92.45581124,95.11162248,96.4395281,97.76743371,80.50466067,81.83256629,91.12790562,95.11162248,96.4395281,97.76743371,99.09533933,80.50466067,96.4395281,97.76743371,99.09533933,96.4395281,97.76743371,99.09533933,95.11162248,97.76743371,99.09533933,95.11162248,96.4395281,97.76743371,95.11162248,96.4395281,97.76743371,96.4395281,78.02790562,79.35581124,78.02790562,79.35581124,71.38837752,82.01162248,83.3395281,84.66743371,72.71628314,74.04418876,75.37209438,76.7,82.01162248,83.3395281,84.66743371,74.04418876,75.37209438,76.7,78.02790562,79.35581124,83.3395281,84.66743371,85.99533933,74.04418876,75.37209438,76.7,78.02790562,79.35581124,80.68371686,84.66743371,85.99533933,74.04418876,78.02790562,79.35581124,80.68371686,82.01162248,74.04418876,80.68371686,82.01162248,83.3395281,80.68371686,82.01162248,83.3395281,84.66743371,82.01162248,83.3395281,84.66743371,80.68371686,82.01162248,83.3395281,84.66743371,80.68371686,82.01162248,83.3395281,79.35581124,80.68371686,58.34418876,59.67209438,55.68837752,57.01628314,58.34418876,59.67209438,66.31162248,54.3604719,55.68837752,57.01628314,67.6395281,53.03256629,54.3604719,55.68837752,67.6395281,53.03256629,54.3604719,55.68837752,57.01628314,53.03256629,54.3604719,55.68837752,57.01628314,58.34418876,53.03256629,54.3604719,55.68837752,57.01628314,58.34418876,51.70466067,53.03256629,57.01628314,58.34418876,51.70466067,53.03256629,58.34418876,70.29533933,51.70466067,53.03256629,58.34418876,53.03256629,57.01628314,46.27209438,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,40.9604719,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,51.58371686,52.91162248,40.9604719,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,51.58371686,52.91162248,54.2395281,42.28837752,43.61628314,44.94418876,46.27209438,47.6,48.92790562,50.25581124,51.58371686,52.91162248,54.2395281,55.56743371,42.28837752,43.61628314,44.94418876,46.27209438,47.6,51.58371686,52.91162248,54.2395281,55.56743371,40.9604719,42.28837752,46.27209438,47.6,51.58371686,52.91162248,54.2395281,55.56743371,56.89533933,40.9604719,46.27209438,47.6,54.2395281,55.56743371,56.89533933,39.63256629,40.9604719,54.2395281,55.56743371,39.63256629,40.9604719,55.56743371,52.91162248,54.2395281,55.56743371,54.2395281,55.56743371];
Y=Y(:);

point=[X,Y];
z=dbscan(point,7,3);  %看z的具体值分为1,2,3,4而-1为异常值
main.m
function [D]=dist(i,x)

% function: [D]=dist(i,x)
%
% Aim: 
% Calculates the Euclidean distances between i and all objects in x     
%                                    
% Input: 
% i - an object (1,n)
% x - data matrix (m,n); m-objects, n-variables        
%                                                                 
% Output: 
% D - Euclidean distance (m,1)


[m,n]=size(x);
D=sqrt(sum((((ones(m,1)*i)-x).^2)'));

if n==1
   D=abs((ones(m,1)*i-x))';
end
dist.m
function [Eps]=epsilon(x,k)

% Function: [Eps]=epsilon(x,k)
%
% Aim: 
% Analytical way of estimating neighborhood radius for DBSCAN
%
% Input: 
% x - data matrix (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object
% (minimal number of objects considered as a cluster)
%
% Output: 
% Eps - estimated neighborhood radius

[m,n]=size(x);

Eps=((prod(max(x)-min(x))*k*gamma(.5*n+1))/(m*sqrt(pi.^n))).^(1/n);


%............................................
epsilon.m
function [class,type]=dbscan(x,k,Eps)

% Function: [class,type]=dbscan(x,k,Eps)
%
% Aim: 
% Clustring the data with Density Based Scan Algorithm with Noise (DBSCAN)
%
% Input: 
% x - data set (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object 
% (minimal number of objects considered as a cluster)
% Eps - neighborhoog radius, if not known put []
%
% Output: 
% class - vector specifying belongingness of the i-th object to certain cluster (m,1)
% type - vector specifying type of the i-th object (core: 1, border: 0, outlier: -1)
%
% Example of use:
% x=[randn(30,2)*.4;randn(40,2)*.5+ones(40,1)*[4 4]];
% [class,type]=dbscan(x,5,[])
%          
% References:
% [1] M. Ester, H. Kriegel, J. Sander, X. Xu, A density-based algorithm for discovering 
% clusters in large spatial databases with noise, proc. 2nd Int. Conf.
% on Knowledge Discovery and Data Mining, Portland, OR, 1996, p. 226,
% available from: www.dbs.informatik.uni-muenchen.de/cgi-bin/papers?query=--CO
% [2] M. Daszykowski, B. Walczak, D. L. Massart, Looking for Natural Patterns in Data. 
% Part 1: Density Based Approach, Chemom. Intell. Lab. Syst. 56 (2001) 83-92 

% Michal Daszykowski
% Department of Chemometrics
% The University of Silesia
% 9 Szkolna Street
% 40-006 Katowice, Poland


[m,n]=size(x);

if nargin<3 | isempty(Eps)
   [Eps]=epsilon(x,k);
end

x=[[1:m]' x];
[m,n]=size(x);
type=zeros(1,m);
no=1;
touched=zeros(m,1);

for i=1:m
    if touched(i)==0;
       ob=x(i,:);
       D=dist(ob(2:n),x(:,2:n));
       ind=find(D<=Eps);
    
       if length(ind)>1 & length(ind)<k+1       
          type(i)=0;
          class(i)=0;
       end
       if length(ind)==1
          type(i)=-1;
          class(i)=-1;  
          touched(i)=1;
       end

       if length(ind)>=k+1; 
          type(i)=1;
          class(ind)=ones(length(ind),1)*max(no);
          
          while ~isempty(ind)
                ob=x(ind(1),:);
                touched(ind(1))=1;
                ind(1)=[];
                D=dist(ob(2:n),x(:,2:n));
                i1=find(D<=Eps);
     
                if length(i1)>1
                   class(i1)=no;
                   if length(i1)>=k+1;
                      type(ob(1))=1;
                   else
                      type(ob(1))=0;
                   end

                   for i=1:length(i1)
                       if touched(i1(i))==0
                          touched(i1(i))=1;
                          ind=[ind i1(i)];   
                          class(i1(i))=no;
                       end                    
                   end
                end
          end
          no=no+1; 
       end
   end
end

i1=find(class==0);
class(i1)=-1;
type(i1)=-1;


%...........................................
dbscan.m

最后z分为5类值:1,2,3,4,5,-1 其中-1为异常值

另外一种DBSCAN 聚类

%
% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPML110
% Project Title: Implementation of DBSCAN Clustering in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% 
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% 
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
clc;
clear;
close all;

%% Load Data

X=[0.851403135086569,-0.473129980972481;-0.0143385521233045,0.689712419407323;-0.0225941463131059,-0.284256368553826;-0.0610040966320611,-1.25288241358493;1.80117233191458,-0.0439530948272436;-0.231990584409464,-0.432152258671540;0.451005664109405,0.509448291585219;-0.332307442473074,0.357188901599257;1.21156610943639,-0.982158613685041;-0.313355175598868,1.14892742489430;-0.926445289673896,0.434611267965341;1.01517568797153,-1.08298578575233;-0.325034731469111,0.241466601791346;-0.0330343244242803,-0.343982552671932;-0.674309830867955,0.783755234640380;-0.328543772237157,-0.533932267968002;-0.991272497729558,-1.28828476707858;-0.440599925733113,0.966849073124444;-0.385467065955120,-0.863615619096165;0.468760528141179,-0.130974595338288;-0.0940298788944022,-0.368448959656644;0.278128169428504,0.0166977609109131;0.756870976420947,-2.08238210433868;-1.03251448268113,-1.40409663914452;-0.0335361385536346,0.456282760690612;0.908341594657170,0.163245561731714;-1.12184369860605,0.0218630545204664;0.403914974186831,-1.24500829463461;1.63585886336799,0.520462800581572;0.804227563945955,-1.59420465573869;-0.0795035652250097,-0.567210793390362;-0.426852729414960,0.0498663598354053;0.167097417826316,0.140841091737349;0.232983900083107,-0.116110172310699;-0.626430792550341,0.0904126767681207;1.36396927463365,0.830663286973774;0.650981874069926,-1.04648165259711;-0.115120160954915,-0.771940334716404;0.000405935112662872,0.743807957304745;-1.52328631722222,0.140467461060478;1.34777598282994,-0.798480973347104;-0.812202840767802,0.0577231924693656;1.33008936403371,-1.23738040720206;-1.30621641686816,0.646472113166677;0.507031978133958,0.444564961953638;-0.0322931942188008,-0.551053273687477;0.546438378197356,0.267465545514150;-0.118587032785411,1.27643799271765;-0.626041951213916,-0.0953139899785633;0.453755347485846,0.287206842874819;0.969664605026431,0.538148559179079;1.25290832188369,0.724626696901346;-0.593143094274061,1.31382181318861;-1.42222383795303,0.893456133536546;-0.0928269476242106,1.40114298326168;1.43240523230272,0.589780856526984;0.448632390674321,0.360859279197181;-0.0132145166157090,-0.337315401470054;0.160810166073833,0.546135033243474;-0.584361142505334,-1.55878801228024;-0.617291333724175,0.699232094934682;0.514928251906051,-1.49364827722076;0.0138635993228995,-0.647257097943863;1.96784561483740,-0.0572828698003273;0.516160021572451,-0.797145733329533;1.29097894351733,-1.48178673794837;1.04574463990739,0.622207181516242;-0.541342049995913,1.00989228438232;0.266109461297069,-1.09642417641248;-0.321706991751831,0.638577488777399;1.40571486588374,1.90425418037697;-0.789699946866524,1.08970735885582;0.138634129269004,-0.815238127350002;-0.897150307330400,0.718335930712740;-1.04743297542305,1.09057535383106;-0.124584470693613,0.319191450115608;0.509434596965920,-0.178013654528075;-0.241363502480452,-1.58204435460770;-0.393667878380026,-0.767655347278958;1.41862767907158,-0.0178488111516978;0.673472141925565,-1.42567312431223;-0.890841125358596,0.637666206759049;0.423864333494803,0.475019480775181;-0.259991105155046,-0.0106233858636353;-0.130087061984227,0.197067831310703;1.19561284700488,0.364166809590991;-1.94965957820543,-0.952605628220473;0.291751861163691,0.666932654967058;1.58444696348414,1.17316410607547;-0.640143553705955,-0.735492050622295;0.602856706078690,0.622660894273971;-0.502034040236210,-0.230244099116707;0.704387852343892,-0.232939843371759;-1.28997533357924,-0.300450201199654;-1.13409261203568,-0.490617621675583;-0.702344450839358,-0.0858600048291331;0.871107465909712,-1.06496578026199;0.897243949885602,-0.438976228624805;-0.527373491940105,0.0394921101386377;-1.14650801804505,1.26512119754662;0.567918245792361,-0.355357276897022;1.15799623581136,-0.776967887125450;-0.934896082923188,-0.521467216013195;-0.129228664439574,-1.39700206278629;0.508766290448633,-0.598958439389555;-0.198796871979583,-0.602536401135011;-0.247375107058653,0.304326391318031;0.876370109529104,0.534235905078014;0.432554038721506,-0.243420716467233;-1.38748303505948,1.27511746693595;-0.446355670979072,-2.26407483829053;1.74627555936846,-1.36485412954923;-0.513761803336968,0.234744127439090;0.795099935849500,0.717521318539757;0.268417366459686,0.626554948757433;0.180576025574037,1.20693420414650;-0.704119098752651,0.666619237836174;0.744820330758183,-0.711729013772122;-0.500694806059865,-1.36166070617287;1.98378777785228,-0.0250690701874545;-0.634674250424264,0.313592259485765;0.0941200155413062,-0.126469434998702;-0.428735988318612,-1.63509566972446;0.128926771644097,0.109926281657495;0.384251308255014,1.13295292685555;-1.13721569931103,-1.79000384345919;0.0175573798653556,-0.643224054595771;0.211619308151105,0.989585510347632;-0.743071682153239,-1.45911124668968;0.835069984028554,0.464020867163188;0.478507287015187,-0.433824957882879;0.813119510272553,0.214205750341315;0.476243613033038,1.46321655548944;-0.0955926750271805,0.621274368658119;-1.22878301784097,-0.115784674252286;-0.150696717394072,0.339924347062621;-0.980360168187952,-0.297085545024994;0.161107001501499,-0.0529868329891070;-0.401711734164833,0.0671615148364457;-0.0292322919416246,-0.414389584185193;0.0434778178816344,-0.0452690528340896;1.79123075811061,0.810392508284597;-0.500551745139609,0.513938176603306;0.885723973156126,-0.386793228061598;-0.288152928860950,-1.05897710421477;-1.00373283327072,-0.392359498102900;0.924633473660674,-0.800384621884143;-1.38188545045619,-0.687715907101494;0.409370162308959,1.21092748761916;-0.812525998614526,0.0610026105461838;0.201769247353032,-1.62185597672931;1.86232458127952,-1.47419168040894;1.68275049278576,0.643177160520134;0.483290009601737,0.185010291331965;0.469852808825871,0.431488159866035;-0.233244138807079,1.31603779047937;-0.303534995820002,-0.406236806290399;0.699057226112228,-0.206808867082432;-0.755932771536502,-0.716841824740523;-0.137295699764704,-1.04540186189125;1.81320616882662,-0.790820320457135;0.0663496042964903,0.343635868655767;-0.359594171255293,0.746750480950846;-1.66638967732583,-0.425965671704383;-0.191810815426262,-1.17106001084320;-0.316826244488455,0.561078069646286;-0.335338990979809,-1.65274445644264;-0.375520307167807,-0.548409685384089;0.0185540177144707,-1.13417378260840;1.01207606935099,-0.617255757867700;-0.876426201835071,-0.203088190747984;0.108300408901506,1.36232687340689;0.00218789417860655,0.695456065780278;-0.939975385263652,-0.358470360918045;1.30283596179283,0.00267788353485366;1.19603735000728,-1.18662920393894;0.598883652191289,-0.169819826784064;-0.227404005546397,0.102797604353177;-0.875802934176858,-0.595126731038317;0.447302960079113,-0.281221128380804;0.512885466919188,0.575249632749204;0.0445923439331762,-1.12672954386968;-1.12011899575360,0.914694885309904;1.36127896848250,1.46091988913459;-1.15032139672951,-0.500652316493059;0.302395012812372,0.198998709083927;-0.228376674204686,-0.0186532769465762;-2.26413788734386,-0.154674932641973;0.0983245448419042,-0.890502758476620;-0.743535328260789,-0.355220458591626;0.775225407152359,0.529698254484606;0.521397000541911,0.603364947325844;-1.33994728142537,-0.751588887188293;-0.354164324053830,0.102188136882157;0.132186983720347,0.430356757002679;0.165259985348386,1.07862563293371;1.14482005993569,0.402539428679472;-0.785735632998866,-1.63496435074993;0.0305706048247976,-0.530333248034426;0.118639963580212,-0.571820153410517;-0.708806171239830,-0.353386333407913;0.909623574333429,-0.363392676410011;-1.10965697438413,0.817111889815803;0.465805598398977,0.0654757013819209;-0.698555330882680,1.55004778736595;0.396236837989394,0.339028132934596;-0.763661645259305,-0.195025346183613;0.245499580353500,0.644082235986448;-1.70207371425037,-0.477237182692036;-1.19117904166530,-0.417016710158252;-0.526564117066081,-0.111032890656210;0.138005640318645,-1.06440514097205;0.116594351703247,-0.348021005077340;-0.0323049001246057,-0.775814564535736;-0.707721487017779,-0.649220376257501;-0.389099505816314,-0.426245466023355;-1.19799491670534,-1.28984100067544;-1.01485068781512,-0.413565839741775;-2.12683641724198,0.00362117099480105;0.145077320240351,1.23746001460342;0.112229210182464,1.11332671975778;0.180481314333538,-0.208473472928401;-0.965537312959112,-0.160568424180516;0.255735457620873,0.965017647824803;0.289719627084579,0.206688640761284;0.145988737820473,-0.923715930413243;1.98054396804911,-0.306124591968637;-0.726560017113136,-0.264835789095823;-0.155557014812085,-1.12373002084361;-1.08717741998012,-0.210611178690292;0.989026334905811,1.28644762900638;1.33807226034067,0.647669477625713;-1.25769047450349,0.971656021885114;-0.105395326725074,0.0690125994803742;1.57600973591844,0.0142522155913231;0.147960545790889,0.891200246842153;0.0446741282095810,-0.275847064332583;0.492601907814685,0.255595112914095;0.545345497350113,-0.631761223068718;-0.885992564081990,-1.54764047690139;0.209205778414068,-0.449855614615190;-0.428171676791103,0.146537871600221;-1.19137157474086,0.798697925875255;-0.947249746787464,0.108196522410801;0.0314690032549208,-1.05089134794703;-0.237987409367987,0.634511453573731;-0.902049700761316,0.00845485578182690;0.437207954411150,-0.526944288805728;-0.657877654237169,-0.519441745264334;0.930453813734077,-0.988775144907727;0.701439005718088,0.225060453653785;-0.284461316050268,0.511329815449617;0.726855088044256,0.240910143000375;0.185428768363872,0.554983168717347;-0.130964505263922,0.0885322851200708;0.343702668273201,0.0636738239952805;-0.210592474865712,-0.286708312869680;-1.08029747648790,1.31990312838720;0.692157298389358,-0.895556720798231;0.288979146079023,0.389798583310157;-0.516321761032473,1.00109889466978;0.227724907151555,0.650379357261649;0.0308171633511083,0.809293140380204;-1.23333715004137,-0.0467919803568150;-1.72837937179382,-0.458197975887880;-0.160147356374789,-0.0310364244979466;-1.98027310513409,0.926394178157440;0.161413729666722,0.562218086877871;-1.16038059701224,-0.429280384760805;0.243490693023694,-1.70701032072271;-1.00808682338823,0.112143540986397;-0.980047083317170,1.51505863657676;1.02844471929902,0.615765847705199;-0.0274402255009172,1.42270113928403;-0.897925206209551,0.618597718562649;0.140286595647992,-1.27169769493602;-0.313272568827778,-0.357696976281336;-0.363214318402778,0.237650877763148;-0.259300390824598,-0.665663616336808;-1.79594072283090,0.0451957847258865;-1.31965564288286,-1.02493798080317;-1.08181631612099,0.342674113967219;0.403951542939906,1.03186526484819;-0.0778535053100324,-0.900682618576575;0.258502322594074,0.289636288963213;-0.655391817580893,0.466489394151992;-1.35452942120581,-0.0418754048760190;0.311132317834387,-0.187851172510864;0.906050267390256,-0.617103881691148;-0.124630635141358,-0.132325570107312;0.925185020139369,-0.573003212848472;0.148812719596386,0.292749475573647;0.985151590740551,-1.24999632364471;-0.581001074657753,-1.48426681642536;-0.501020971654043,1.69177648437272;0.371547688531884,-0.437693390364218;0.988095322954662,-0.0879948893747378;0.121298959265064,-0.198744712866603;0.887642438363742,-0.788460574647595;-0.0457735631063480,1.35332211051316;-1.40752089456371,0.769038924727587;-1.01252771389921,-0.355116255672589;0.547883504573530,0.509816562921687;0.611165500676238,0.613124562309603;-0.0119514372028091,-0.748473658670974;-0.233059936855048,1.22489739639392;0.390311659722373,0.334572544928212;-0.603423173446973,0.832584814357223;-0.0144740780100697,0.0436679647872782;-1.03832858877526,0.475498022444234;0.301811834186723,-1.20144297957673;-1.22405364178429,1.11612764309048;0.286334721806747,-1.72072610755022;1.12474539695135,-1.28280237921981;0.577468857193040,0.0637225761001807;-0.501303417781672,-0.719976066042957;-0.626138197461824,0.0682986788401482;-0.268874805682798,0.708886270533125;2.06754664154143,0.672240183289361;1.49233173597889,0.353691023550336;0.256879352781016,0.581784465102164;0.172512445812608,0.661587059442096;0.309431745524189,0.674977907424030;1.61699294794098,0.624278931007399;0.290974673668630,0.758916898522239;-0.257503465414993,0.536412562702471;-1.21593287418458,0.542645059345448;1.30260058914871,0.139305417472207;-0.0457031470538969,0.0123548578169672;0.293727678940304,0.0577349470882062;0.624474038903530,-0.161885032793345;-1.25297481829255,1.88688811256307;0.726254609428325,2.06914430419161;0.322767052970911,-0.547535451610764;1.49954596821641,0.619651294189132;-0.469883316352418,-0.419660970848882;-0.570817054505356,-0.101542300710940;2.04363476084141,0.590400222793054;0.253286112898015,0.584363205016504;0.405379682116085,1.04545562523930;0.610892327830635,-1.43420845986422;-0.918012045486365,-1.46733131156152;0.689061838189169,0.970313638863159;0.883171311012425,-0.153232801979999;-0.859836253888845,0.521795040124635;0.715578756624138,1.29077975190934;-1.29361857696713,-0.335099490963216;0.267679212516649,-0.408678402572121;0.413024181704813,-0.112501923280020;-1.50587448728282,-0.289166703089680;-0.202002142776031,1.29224356388910;-0.959194830835450,-0.793319155601430;0.418565282728189,-0.923483663503886;0.198246915782785,0.186560174972332;0.00129122963879560,-0.0289161225675570;-0.288552147123351,-0.520920320030107;-0.572006649839543,-0.0737259613009863;-0.719324134512467,1.22596629733364;-0.133388714044536,-0.132359296963796;-0.559824118762576,1.35923147038287;-1.74481441278829,-0.429764793022444;-1.00915367421324,1.18882766232097;-0.452007628823371,0.0481212629140170;-1.05930563002742,1.16737604344696;0.416741208367106,0.446044724208292;-0.481551073849327,1.35277324964743;0.484457889379757,-0.182264899279516;-0.608291197454682,0.442777685817525;-0.188059439608459,0.711403215695584;-0.202736286627045,0.428133208286628;0.345935463529611,-1.25644349611763;-0.213558393658958,-0.614394002341074;0.795063773939893,0.932146415148329;-2.03456341253553,-1.15127671577875;0.392131219480001,-2.86956517472613;1.52846314674513,-0.698857767284948;0.231447090332427,0.857666153778464;0.0254736815851833,-0.886602410897202;-0.112144350834825,0.797441286789398;0.486239077299172,0.0308449908745703;0.152639474310169,0.291058179728814;0.697041140314914,0.146760088034139;0.921919254555835,-1.53564033242104;-1.09162091416072,-0.725032441658203;0.461230868982243,-0.702632458982111;-0.762308721681700,0.188661984919519;-0.0267861502951600,0.336300135519108;-0.317380130019386,0.414575239434657;0.257847170699358,-1.13312527493475;1.58538948411166,0.939676588366702;0.239705332771800,0.298375868807153;-0.875877828834379,0.931497210530428;-0.125937469389768,0.161502431711115;-1.18799027046427,-0.593923848324424;-0.348282147403970,1.57621031070866;-0.551207522515835,2.31233012385174;1.58850178215842,-1.16939468492785;-0.318827958684215,0.341886565617294;0.301667399945684,-0.218031945425233;0.805420696145366,0.184675366691312;-0.646766502589300,0.303745780452822;-0.289660794190369,-1.04683232142510;-1.46701127659970,0.339954975780579;-0.851434753950484,-0.109922143626028;0.400358554065480,-0.278374258150974;0.553882718769781,-1.29083464321456;-0.291817761095059,0.210482284590927;-0.618010502756651,-0.115809039528780;0.143292539534772,-0.391567904466992;0.125998843916864,0.334489269100786;-0.146001925395615,0.910323744381836;0.648806377178966,-0.320570784154905;-0.699682482191375,-0.119226026059734;-0.157104294509116,-0.931339297931336;0.0801828253410317,0.454005866908870;1.92052139372240,0.372033256350640;0.979463158436656,0.118935877854764;-0.000779447856084988,0.654077297754245;-0.332578370370696,-0.0148867376913024;-0.902322915907549,0.768120682832945;0.260221380036246,0.745567136069740;-0.708035228625356,1.30252566337368;-0.151612994523752,-0.0695820881429840;-2.32219452210224,-0.728600894874358;0.600720928289906,-1.19500532344365;-0.0753290178918703,-0.327310959674125;0.127300712512011,0.362251709096736;-0.206313491702423,0.224142564975490;1.08941378447522,-1.14870416664216;-0.102157462569054,1.12114084135180;0.432081199693788,-1.69271069286813;0.131215057926959,0.268710334026202;0.175419184722903,1.94134917319629;0.259695458407081,0.0231250622928163;-0.530654900177502,0.978594476486771;0.908152844187588,0.143289020169843;-1.78346757177442,-0.523586451447988;0.242986393359016,-0.308389229042762;-0.236019282244355,-0.342654640298142;0.286981789554352,0.985437071726386;0.107357246650536,-1.27779884209298;-0.291088955200243,-0.782237538189678;-1.48838869390190,0.610480261635520;-0.290428641904365,0.449393032743737;-0.124246773574067,0.524294208542101;-1.10911309794130,-0.922642789925004;-0.266615122954708,0.0436078655289662;0.0674109443499525,-1.30356408272809;0.731210224052585,-0.299424682168368;-0.0783140011179236,0.984867530517952;-0.0671132912319112,0.134076483025846;-0.364067122671229,-0.722874921131597;1.51439439837422,-0.0771940802024117;-0.996239031400006,-0.247085458237878;0.781994779770694,-0.635290896163928;-0.415136775403776,1.02903264950054;0.261975576403215,0.227241434864270;0.105179902224613,-1.30655136783898;0.131078831076714,1.19726898912671;-0.572431312697528,-0.517738110135124;0.761193115065827,-0.151528430075772;-0.700787892018522,-0.125817925825309;-0.710523515085594,-1.14743494048308;0.271725446742974,0.772344368086419;-1.00885937023322,1.50930568190961;0.634348610923551,-0.546333228806681;-0.968929647938074,0.975795787080948;-0.213074124452758,0.406380732935223;-0.760407627561462,-0.462600816382117;0.0227641270806949,0.499118388541767;-0.959163904961547,-1.27371697507166;-0.181799908314369,-0.317001719956157;1.92695528998332,-0.245442915649609;-0.467693995266174,-1.07492941887782;-0.481078749641947,0.0480267761530564;2.58614731615451,-0.221955824066255;0.516511860243649,-0.919767943126101;-0.938728713659267,-0.496212780312738;-2.00052142463382,-0.867698132557368;-0.307549905884066,0.506046051241367;0.891452945277505,0.867061423703767;0.0225117147340936,0.596189724647175;-0.00284388720760158,0.938964136512649;0.542444705306723,0.930504333514282;-0.398606019904522,-0.836212527633248;0.624134747649245,-0.892087592670498;0.174325534082661,-0.399241410096260;0.578516076217968,2.27286393319520;0.368006030992534,0.970820131777525;-0.743635644009496,0.404244454419487;-0.0629951619608784,1.32415709008755;-0.401514889609861,0.0470256506472721;-1.54759474103388,-0.316654798982184;1.54605390698592,-0.233088332737206;0.518459980080741,0.326038971090102;-0.0484911809391796,0.568328506879675;-0.463899973717016,-0.265386373267296;-0.466383396849741,-0.145294301176185;0.160056442839050,0.199649543916765;-0.577544831943327,-0.348426759562846;2.50636262096189,-1.89942385825356;1.87962131484011,2.78971840907837;2.17528438813680,-3.10117825885630;3.24234894810685,2.24978693293942;2.42327181877812,1.90342341110681;3.29283973093715,2.08931278675576;3.40979093479647,1.68208811315795;1.34004449580202,3.49243122026193;0.503016464294754,-3.24483387006166;0.331608982467335,-3.67672234296917;0.182200361178451,-3.09145137216155;3.85938766092990,-0.978023269732328;2.44181487598509,3.04044056846152;0.594531454804245,3.79943389075634;3.59697018928767,-0.615183209784243;1.12086249976746,-3.50908755094999;0.603439893242363,3.40872455596613;1.34071004930712,3.58894051518642;3.59293385548593,-0.442464674327149;3.87903481973303,-0.164894281347844;2.66514757497131,1.50939335867939;0.998144879389831,3.51845333774610;3.96432503009658,0.0931124909736951;2.19640125460340,2.16547446521701;1.80275453311028,-2.59823243798084;2.68692704734840,2.47216499010988;3.21964781432772,0.370271472285136;1.53849476488092,-3.42399709316578;3.26764851357532,0.314469521304579;3.20731967128992,1.15703487994881;3.44177788297025,-0.496993239232624;2.51022078983829,-1.67441290844976;3.69147624543048,-0.0266369126936839;3.50552341027405,0.678876823160937;1.29388550396712,3.71141751429471;1.73980794220564,-2.81247427172301;2.81335975850807,-1.12971995634260;2.11412475403236,2.84903626317894;3.52006137236472,0.0746347930328961;2.29451845656957,-3.01249827876675;2.25928510933760,2.88707733916192;1.66130362223689,3.62362637793229;0.519628965109547,-3.65486150626913;3.38647428095931,-1.06519588052522;1.18772047293491,-3.42929667216109;0.312821809015028,3.95801048962959;2.63549401816953,-1.67955798595466;2.85772707691467,1.68870021678031;3.35816007074702,0.174892898516875;0.139428513540717,3.71449030848393;2.62737473021890,-2.74267037354551;2.20969610475619,2.37773149906467;2.84315577971697,1.58361751138297;0.495654648775707,-3.94198972095221;0.0386451443921792,-3.97052144740946;1.24796287156667,-2.85421423372850;3.85424961145068,-0.966523212465791;0.210741371962373,3.81106396311375;1.45726025322288,3.30150262421560;3.22206119518507,0.625385084442001;3.31700309569925,1.70892983985444;2.93954237258192,-0.742539116917386;1.69523275652458,-3.34219521970605;2.57136881960244,2.98735595219581;2.71330930116607,2.24954521415671;2.32650287265209,-2.83150904246774;3.52077232410432,-0.585604976410609;2.52357083942858,-2.93321553995649;0.327185032168852,3.28038206246899;0.854474823218118,-2.88689184027279;2.84337375164720,-1.91265530305504;1.73486346520254,-2.72919861786728;3.32955360987617,0.893637066672383;3.42417639351157,0.871850699187576;2.67626580222948,-2.39340036391807;1.48234793967205,-3.39195121648216;2.58745165167074,1.55495372328340;2.06888788751884,-2.31293560222615;0.455548062820739,-3.61247887363246;0.499374423387897,-3.83705142350255;0.412920563794504,3.20482113017584;3.24822271943797,-1.10516525598822;3.90990492614712,0.783301659693743;0.0182578547422773,-3.17687102492541;0.132082996295263,-3.71356618380902;1.96566516553549,-2.30620527749875;0.147408906946268,3.15366968179161;3.30000193467696,-1.57877200360419;1.05026399213323,-3.45130187002808;3.22391683089953,1.73083877458731;1.86626597310605,2.90126015814668;1.45326842460568,-2.84782145383519;1.06890373237855,3.02391764678640;3.34675640799309,0.103799255997186;2.71557209128150,2.51937611677325;3.09315444883807,-0.218148694141439;3.55229065003612,-0.395667385066330;0.669282343132067,3.77631938835593;1.46251660781193,-2.68521379893275;2.40771606524689,1.90123074384870;3.08203644230162,0.452033174498522;2.13563762279879,-2.16619217308896;2.15011912970370,2.48506143313723;3.20600865758244,-0.409716636885783;2.84454799996086,-2.69343529123031;0.147544275713420,3.37141164783944;2.77701621649794,2.32913297661622;2.86923537155419,-1.98794379210268;2.90927916331057,2.30583626890552;3.56875786757035,1.69883710973537;1.61688053217233,2.75110529000491;1.59577039357033,3.21486727238053;2.82474765375342,2.45923946166249;3.14343595014819,-1.06735122951126;0.786563257237719,-3.64653407758490;1.80070841410158,-3.15282490375481;0.150538434577198,3.89766890447707;2.34308604265805,-2.48482077396721;0.690417052188859,3.21311715880355;1.27334941961779,-3.73173321728926;0.484811391554708,3.29169200219898;0.334217418813795,-3.98322004263661;3.26786384673329,2.25842469241670;2.76422072579666,-2.54461496540284;1.89732389099812,-2.52223057448585;1.02589621457424,3.67173940538823;3.14504488708508,-2.43618883530266;2.51996085201696,-2.28368871032699;3.10978982643776,0.0642022338589866;3.43582249541077,0.895853022124102;0.333124603465335,-3.62225350047210;0.730891926867816,-3.71993524563904;0.0434662123602413,3.05808064520265;3.18907754052442,1.08402728729502;1.97163068607520,3.34693129136609;3.35837439294503,-1.53139242776510;2.46161070614418,2.58430351013621;3.69277390881793,1.37208992150567;1.43441983155036,2.97879190836624;0.853976805684344,3.70194283035668;3.38770676622316,0.172591422440824;2.61972880932436,2.94968491879348;1.98734943590096,2.58153623361927;3.32713030344231,1.29043524003239;3.18604476467494,2.20360227269285;3.29368836799270,-1.64392595517454;0.721280180008247,-3.16834032250622;3.27458697139280,1.46818532065983;1.73666218030615,2.89355628440327;3.92388051970659,-0.730839831974859;0.894423656928309,-3.60343074345482;0.838281147595819,-2.94144137400160;1.12264219961692,-3.26008354780063;3.72442348086047,-0.434171666113178;1.55848884381464,2.67464836281407;2.17676719096837,-2.46453408173964;2.73648478208293,2.10543428541784;3.55299493884692,1.54952683598939;2.90849969729371,2.50954270965111;2.27206425351042,-2.76137137520563;3.32240353243831,-1.80439933910344;3.36182697025385,1.35072518947710;3.28113548532655,-0.638426155744990;3.05137565739563,-0.167219858907298;2.69751886257075,-2.63235544250766;0.739172181881971,3.29625725607374;2.04816725908900,2.32918916927412;3.04622846991362,-1.94960543859827;3.24742181941953,-0.0275069558526382;2.48645243611671,3.11051619804141;1.78243284493335,-3.48100658850678;3.32823449689098,0.953920920621467;2.28629723080597,-2.08873809599526;1.07270439338200,-3.65502330920649;3.50575462426683,0.247007392542740;3.14995754953498,2.13769314772725;2.96936122422286,0.583418067690417;3.64592778996880,0.262339509338639;2.75602889977173,-1.90357196291558;3.88618445682927,-0.286317822348824;1.80273783051532,3.40329915048116;1.00726760893516,2.98088953052271;1.97421246398303,-2.58389384574453;3.41277696952062,1.63826439249251;2.36158863796876,-2.52076509029459;3.50114471876005,1.42751691321410;1.17347660370629,3.61225171871170;3.27809846124457,-0.0163729297247401;3.76254616296789,-0.879119663064399;0.781288886659030,3.92235395490442;0.104449728503339,-3.80225348945569;2.70703193857219,1.60856548952329;3.23199496754078,1.48316906575607;2.64234851327251,-1.95888382149626;2.97780800249331,-2.48387790429270;1.92197303651388,-3.23481516349732;3.54374695630894,1.23684562840990;0.882567426033693,3.84941889618365;2.06388173754818,2.91326665057852;2.59433928371902,-1.70200030870194;2.60240748708211,-2.48174065171952;2.86937336981601,-2.69295485484413;3.95162000847664,-0.307260980095071;3.60299525870480,-0.616473595406715;2.01827441767634,3.28182884178518;3.52829968631123,-0.0394764942480208;3.11294700361764,0.422909633011402;2.12798087583777,-3.10000387784011;3.33172380846689,1.60950350012154;3.22727866878794,0.591180928865557;2.43795154290812,-2.44061402510829;2.46986746065783,-2.94632120974892;2.29452613398239,2.44399534069504;0.0915571122488904,-3.96083368939130;1.35829434136235,-2.86705257827584;0.403586606043209,-3.36513638112493;3.26345629197559,1.96196797053646;2.49654102536469,-2.17004536929496;2.76558292155813,1.35376550088280;1.96560627605496,-3.29588324968602;2.76490505559721,-2.38741277467547;0.326584940063399,-3.93505845204445;0.995479484852253,3.05907265800787;0.810396172995996,-3.50352843504663;0.852077493694365,-2.95882610427918;3.82974322687783,0.109674165524733;1.81239271605249,-3.26955251168847;2.03944531106603,-2.67138705005603;3.02702439142643,0.916704363768343;0.0285531317185697,-3.85876627130763;2.53012468276583,1.66958957479007;3.43646422533189,-0.608621919031831;1.11260384351599,-3.01843365553896;0.720681079198994,3.18251417563157;2.96991639035171,0.711257290335792;2.28107934881569,-1.97244084954149;0.0724405438780383,-3.75407639180335;3.20418324558626,-1.69111756766082;1.96095823435855,2.93925093527234;3.51953445347597,-1.49509063490060;1.56054737244866,2.85949685533991;1.05101801037240,3.64931712182901;3.54607964386966,1.04929063620622;2.82758107865437,1.03323232913140;3.73044746777372,1.30053765228263;0.964389662777977,3.25984843405740;1.49948244885236,2.64430780510397;1.71802695385379,2.95539621603167;3.33936556272460,-0.395629984118512;1.49986362602387,-3.34771077954852;2.96757302513546,1.92757998063022;1.03235332068958,-3.77115172156358;0.719901237980640,-3.75430488763412;2.60626147213679,1.78789541665804;3.29483466920804,0.239422995413907;0.809310704864449,3.05236529312655;3.32577539563462,-0.471743633798372;1.90106319466384,2.39695438380257;3.49016410837793,-1.39281953677730;3.83540444776637,-1.12399184223289;0.885538525896092,3.59218590527892;2.33218534988196,-2.35460468538867;0.506697162469077,-3.30230037086417;0.322519771460947,3.81570495826783;1.18844527973337,2.96906114237162;1.99609358001446,2.59445757895823;2.99789120122207,1.57549123644103;1.40561604938716,-3.31389604012088;3.22389227160342,-0.591602037269366;0.0290490016137027,-3.00265777467438;3.46949389006918,1.55360707269484;2.67656113815520,1.37152913956380;1.62865878747941,-2.75537986774211;0.847111359299475,3.43021518479660;1.52405325485597,-2.82153131750285;3.82565718635010,1.03432202028943;2.95019048784688,1.89429413865117;0.906201686705693,2.91543413968344;3.38675575233901,2.02305545638741;3.24863595441122,0.609795562692978;0.277310850910882,-3.47809399936050;2.44096393394337,2.59721820591226;3.68449434162543,-1.28323633335205;3.65473666865905,-0.591909314518796;2.37321079654223,-2.72068444728110;1.77766149565257,2.45387617778945;0.939528871231591,3.42898353963072;2.77428461077442,2.18895007080373;0.0971481393998998,3.78603190327318;1.57500042809470,-2.77455533988130;2.56506215261148,2.38564351627127;1.96947935716934,-3.20114245055684;2.26348951584813,-2.05021457645603;2.41347684236049,-2.22688112254028;0.358598680352257,-3.42828921727791;3.45571788296071,-1.30204372353031;0.156252892028417,-3.10628960781999;3.50739749445713,-1.81386923938321;3.11795784265912,0.0517066167139473;3.37355072982825,0.350029928789100;3.19200920915943,-1.00528426209468;1.44059556470260,-2.95749321622810;1.61171653804549,3.23485603757322;1.54108524536401,3.43214121885218;0.523956350000632,-3.21669228786401;0.461951766378267,3.37008247703125;3.57527462634087,-1.58943167322859;0.00706892000230694,3.05582927575193;3.88594482797204,0.539556277111974;1.75315731514773,2.49837205160410;2.34811579304742,2.93764780653144;3.06150648006453,1.76300948112595;0.920961134537073,-2.86618173521486;1.52732289468026,-3.59009774507577;3.00612237668397,-0.599531997407817;2.08299281206611,2.56801206148363;2.91020726585572,-2.60575016836699;2.61681850689651,-1.54700785947746;0.915784574267994,3.57052476341539;3.26311735880874,-0.189463553908210;1.93857535788665,-2.51593417253321;1.37085022787810,3.44819343477288;3.13026812926081,1.30158372914729;3.26570015753999,-0.924079950310143;0.550654808266459,3.50643055810641;0.824627676524710,3.39320120640982;0.454114151589074,3.54871566310052;2.91820257367542,-1.89590411408329;3.12591675586440,0.100383019809462;0.693138130767374,-3.75211302274715;2.81490620331459,1.12393428713334;2.35631681392796,-2.22008376896145;2.70307553653954,-1.80304065994565;1.10055110802597,-3.73850384294599;1.51315743909015,-3.15516770117787;1.52515157186249,-2.98582610314353;3.26257379183509,-2.17078443321525;1.48538256954141,3.16065588028470;3.12048829405827,1.58728600368996;2.38073211688346,2.54992509456910;1.41905772437759,-3.39690367915974;2.16365966897779,2.39692820789563;3.57657651860491,0.312935553463586;1.58030013122709,3.15614286559982;3.14357828930008,1.64692632428819;3.42514666277590,-0.786215210114303;2.26176778295442,2.82737336510984;2.44094481632933,-2.77365995967657;2.73583232078201,2.75045420258452;3.32723392669521,-0.725730684718124;2.42676377283066,2.66842102089042;1.54343105189006,-2.85222944801109;3.04592258659412,-1.73421114834125;2.94967826666576,2.48427521381857;2.85353429740204,-1.09762080986109;0.900007681237009,-3.57699898375178;0.474593780337756,-3.21296478550370;2.07458923322222,-2.33763406843864;2.03279757212724,2.34141157455353;2.19904598051941,2.81370926545303;3.31430030363144,1.66915003406199;2.53382628235163,2.50432169098483;2.47694068574261,1.84725209300162;2.85955310025611,-1.52635066723478;2.04404999197931,3.00640219082134;2.71265198627477,-2.86934163432507;3.66608259733767,0.528095325745891;3.24828565110035,-1.01993423301300;3.13958029888160,0.280183228458656;0.325301984280663,3.13575396023191;3.65267659673428,-0.146303806487246;0.986207551818497,-2.87058416586563;3.31589778457185,-0.707393296020698;0.845466331788844,3.58654583823888;0.693166420246851,3.89178276033790;0.804040524936496,-3.32573955666568;1.96499411449994,2.58729000017185;3.43650612007088,1.82189640854091;0.890742019283228,3.08846162674753;1.31921747015472,3.37248798993979;1.69326613949465,-2.90983689226790;2.91833672455277,2.63679785329278;2.14673404572720,-3.11010137448842;3.51057412006078,0.265710341558001;3.78173339235232,-0.516715230074070;0.393661179377305,3.63149079115054;3.78757432078085,0.999869125833414;0.463158153242118,3.85805952033143;2.88518432361403,1.83342294770483;3.69321755794361,-1.14969228385001;1.19335483364801,-2.87044856394974;2.99688440559272,-0.309174765550434;0.562657490420588,3.66108151176270;1.03973146819926,2.91619526377577;2.97134576108594,-2.50035330944354;3.07360911322423,-2.19520715953131;2.11912770240408,-3.01350013323453;3.23035861133314,-1.12709464164619;0.421358720102852,3.93096514437778;3.38634905946038,0.834414192797257;0.159033504023550,-3.78505552493495;0.861476952098637,3.37071347144006;2.44997195405424,-2.37687199245959;3.08941289445182,-0.470626270559847;2.99157384579142,-1.62882364645632;3.41191954506540,-1.20338622007807;2.91001519542352,-2.11998242762370;0.384240207954900,-3.89416444279740;3.77574834200778,0.471661465047966;0.870661418558146,3.21559631281954;2.79737226452300,-2.24418235594860;1.00201959042550,3.52939936821678;2.91932072830026,0.963617492962181;1.43943127375298,-3.28349613456327;0.182263819567631,-3.48274127530222;2.35388387727373,2.20291891542584;0.538040623985135,-3.87510856320251;1.99662934330719,-2.27408150566080;1.76861063251559,-2.90669221094155;3.90088912509106,-0.181495223953174;2.39922652034344,2.04269230271155;3.28495700008493,0.564882884313925;2.30416918620708,-2.55232009350752;2.33567383458280,-1.92133673056224;0.869087803012462,-3.08581618577822;1.11878151019479,3.81937075841475;2.93213615037513,-1.04242169032502;1.11602671612818,3.36349999929561;2.58114603513705,2.44979477353437;0.466456630939825,3.53463774126002;2.08423796584590,3.30187047755605;2.83571297211432,-1.81141774565019;1.55916352847108,-3.60369796722567;2.20118529686781,2.61380774113848;0.725124881227123,3.68602534396054;0.0755302830916422,-3.98725018152939;0.829405843486427,3.32978223113251;1.03454797228577,3.44560711637221;2.59186216217100,-1.75737635260968;3.97045066774063,-0.221673719715225;3.78467554659006,1.01891170346449;2.10916396117137,-2.15201021666057;3.54449184789358,-0.762229564625836;3.73175645381035,1.32337600450968;3.88655111213065,-0.295186463635937;3.76952393040979,-0.284104527748060;1.75864235082405,2.94156672801123;3.40525385714602,-1.48352457697981;2.01828490456138,2.32017347246394;2.50907967368258,-2.57880864195637;2.14029079450174,2.93426368894506;2.07819271164718,2.66814116616381;3.47933636689820,-0.364753108915407;1.10058412422293,3.21579518644140;2.82250137758155,-1.02029031910407;0.558285390851468,3.70164230651127;2.06647959408039,-2.66336702025777;1.79001617927184,-3.22893883464239;2.57940275253985,1.81418806875356;1.88801926101617,-2.47118149572031;0.278826760994266,3.03881125179494;3.21357315357524,1.46189821417636;2.04843361467857,-2.38787727423149;2.44391262732498,-2.30997179886955;0.736751073822116,-3.77676579170793;0.535472024619141,-3.08942736346693;0.0931673964369424,-3.20471508209136;1.56474250575112,-2.80667684434910;3.13073907386382,0.859496584606857;0.956797988542228,-3.43743052486543;0.776191294045228,-3.76666011446764;3.52952440300670,0.437171014799549;1.69031189741988,2.77181588696744;2.87437925741902,-1.41380681577554;3.58035359194581,0.233342227263888;3.51976929039632,0.350494785845198;1.38978602949240,2.81532371988571;1.69271232922631,2.53254503290475;0.718897553825410,-3.40855444283847;2.42394661227993,-2.77157293168499;3.02684811007434,-0.528309962197614;1.71304294026489,-3.56181080780288;3.21806092286063,-0.632010480051040;0.634896151784038,3.64263707712815;2.53483152621102,-1.85207247123956;1.19978900087310,-3.74804659947430;0.465967038897157,-3.69557540239029;3.66731884403764,0.772475854931132;0.333410789518809,3.39432281138413;2.60930712603746,1.97438994745331;0.782940627483077,3.18520815144984;3.66534208013442,-1.12113356621199;0.884518247208177,-3.08217331923253;2.20078617775774,-2.57280121675197;1.77665148778885,3.51693575674430;3.77719518444492,1.27037184850614;0.521815852178664,-3.32973517981035;2.20571199154688,2.04493467928189;3.03811922878280,0.756201463023881;3.40522658382353,-1.21781323941303];


%% Run DBSCAN Clustering Algorithm

epsilon=0.5;
MinPts=10;
IDX=DBSCAN(X,epsilon,MinPts);


%% Plot Results

PlotClusterinResult(X, IDX);
title(['DBSCAN Clustering (epsilon = ' num2str(epsilon) ', MinPts = ' num2str(MinPts) ')']);
main.m
%
% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPML110
% Project Title: Implementation of DBSCAN Clustering in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% 
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% 
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
%

function [IDX, isnoise]=DBSCAN(X,epsilon,MinPts)

    C=0;
    
    n=size(X,1);
    IDX=zeros(n,1);
    
    D=pdist2(X,X);
    
    visited=false(n,1);
    isnoise=false(n,1);
    
    for i=1:n
        if ~visited(i)
            visited(i)=true;
            
            Neighbors=RegionQuery(i);
            if numel(Neighbors)<MinPts
                % X(i,:) is NOISE
                isnoise(i)=true;
            else
                C=C+1;
                ExpandCluster(i,Neighbors,C);
            end
            
        end
    
    end
    
    function ExpandCluster(i,Neighbors,C)
        IDX(i)=C;
        
        k = 1;
        while true
            j = Neighbors(k);
            
            if ~visited(j)
                visited(j)=true;
                Neighbors2=RegionQuery(j);
                if numel(Neighbors2)>=MinPts
                    Neighbors=[Neighbors Neighbors2];   %#ok
                end
            end
            if IDX(j)==0
                IDX(j)=C;
            end
            
            k = k + 1;
            if k > numel(Neighbors)
                break;
            end
        end
    end
    
    function Neighbors=RegionQuery(i)
        Neighbors=find(D(i,:)<=epsilon);
    end

end
DBSCAN.m
%
% Copyright (c) 2015, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YPML110
% Project Title: Implementation of DBSCAN Clustering in MATLAB
% Publisher: Yarpiz (www.yarpiz.com)
% 
% Developer: S. Mostapha Kalami Heris (Member of Yarpiz Team)
% 
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
%

function PlotClusterinResult(X, IDX)

    k=max(IDX);

    Colors=hsv(k);

    Legends = {};
    for i=0:k
        Xi=X(IDX==i,:);
        if i~=0
            Style = '.';
            MarkerSize = 12;
            Color = Colors(i,:);
            Legends{end+1} = ['Cluster #' num2str(i)];
        else
            Style = 'o';
            MarkerSize = 6;
            Color = [0 0 0];
            if ~isempty(Xi)
                Legends{end+1} = 'Noise';
            end
        end
        if ~isempty(Xi)
            plot(Xi(:,1),Xi(:,2),Style,'MarkerSize',MarkerSize,'Color',Color);
        end
        hold on;
    end
    hold off;
    axis equal;
    grid on;
    legend(Legends);
    legend('Location', 'NorthEastOutside');

end
PlotClusterinResult.m

附件 mydata.mat下载

原文地址:https://www.cnblogs.com/clemente/p/9592656.html