image thumbnail

Rapid Centroid Estimation

by

 

10 Sep 2012 (Updated )

An efficient particle swarm approach for rapid optimization of cluster centroids.

distmat(in,x,distance, mu, sd, P, s_in)
function M = distmat(in,x,distance, mu, sd, P, s_in)
    nc = size(x,2);
    vol = size(in,2);
    M = zeros(nc,vol);    
    
    switch lower(distance)
        case 'euclidean'
            for i = 1:nc
                M(i,:) = sqrt(sum(bsxfun(@minus,in,x(:,i)).^2,1));
            end
        case 'sqeuclidean'
            for i = 1:nc
                M(i,:) = sum(bsxfun(@minus,in,x(:,i)).^2,1);
            end
        case 'seuclidean'
            if(nargin > 5)
                in = s_in;
            else
                in = bsxfun(@rdivide, bsxfun(@minus,in,mu) ,sd);
            end
            s_x = bsxfun(@rdivide, bsxfun(@minus,x,mu) ,sd);
            for i = 1:nc
                a = bsxfun(@minus,in,s_x(:,i));
                M(i,:) = sum(a.*a);
            end
            M = sqrt(M);
        case 'sqseuclidean'
            if(nargin > 6)
                in = s_in;
            else
                in = bsxfun(@rdivide, bsxfun(@minus,in,mu) ,sd);
            end
            s_x = bsxfun(@rdivide,bsxfun(@minus,x,mu),sd);
            for i = 1:nc
                a = bsxfun(@minus,in,s_x(:,i));
                M(i,:) = sum(a.*a);
            end
        case 'cityblock'
            for i = 1:nc
                M(i,:) = sum(abs(bsxfun(@minus,in,x(:,i))));
            end
        case 'minkowski'
            for i = 1:nc
                M(i,:) = sum(bsxfun(@minus,in,x(:,i)).^P,1).^(1/P);
            end
        case 'chebychev'
            for i = 1:nc
                M(i,:) = max(abs(bsxfun(@minus,in,x(:,i))),[],1);
            end
        case 'cosine'
            twoNorm_x = sqrt(sum(x.^2,1));
            twoNorm_in = sqrt(sum(in.^2,1));
            norm_mx = twoNorm_x' * twoNorm_in;            
            dotproduct = x'*in;
            M = dotproduct./norm_mx;
        case 'correlation'
            M=1-corr(x,in);            
            
        case 'spearman'
            M=1-corr(x,in,'type','Spearman');                
            
        case 'hamming'
            for i = 1:nc
                a = x(:,i)*zeros(1,vol);
                M(i,:) = sum(a~=in,1);
            end
        case 'sqpearson'       
            M=1-2*corr(x,in);          
            
    end
end

Contact us