function [ C,index ] = ClusterOP( X,K,w )

Cstart = kstart( X,K,w );               % kmeans++ 初始化聚类中心
[ C,index ] = kclustering( X,Cstart,w );   % kmeans 聚类

end
function Cstart = kstart(X, K, w)
[n, p] = size(X);
S = RandStream.getGlobalStream;
% Select the first seed by sampling uniformly at random
index = zeros(1,K);
[C(:,1),index(1)] = datasample(S,[1:n],1,2);
minDist = inf(n,1);
% Select the rest of the seeds by a probabilistic model
for ii = 2:K
    for j=1:n
        mini(j,:) = min( vector_OrderPermutation( X(j,:),X(index(1:ii-1),:),w ) );   %按照距离概率抽样
    end
    minDist = min( minDist,mini );
    denominator = sum(minDist);
    sampleProbability = minDist/denominator;
    [C(:,ii), index(ii)] = datasample(S,[1:n],1,2,'Replace',false,...
         'Weights',sampleProbability);        
end
Cstart = X(C,:);
end

function [ C,index ] = kclustering( X,Cstart,w )
[n,p]=size(X);
[K,p]=size(Cstart);
index = cell(K,1);
Pndex = cell(K,1);
% find the nearest sample of C
for i=1:n
    [ d,P ]= SimpleSide_vOP( X(i,:),Cstart,w );    %单边距
    up = find( d==min(d) );
    index{up(1),1}(end+1) = i;
    Pndex{i,1} = P{up(1),1};
end
% update centorid
for i=1:K
    cc = index{i,1};
    s = Pndex{cc(1),1}*X(cc(1),:)';   % 列向量
    for ii = 1:length(cc)
        s = s + Pndex{cc(ii),1} * X(cc(ii),:)';
    end
    C(i,:) = s' / length(cc);         % 行向量
end
end