Code covered by the BSD License  

Highlights from
DM Utils (data mining utils)

image thumbnail

DM Utils (data mining utils)

by

 

15 Jan 2012 (Updated )

The tools for dealing with distance matrix, improving data mining capabilities

handler=name2fun(fun,mode)
function handler=name2fun(fun,mode)
% Converting the name of a distance function into anonymous function handler
% (self containing) for use with functions generating distance matrices
% pair_dist_par or pair_dist_seq, future versions will provide support
% for functions such as pdist as well
%
% use:
% handler = name2fun(fun,mode);
% or
% handler = name2fun(fun);
% where:
%   fun - a name of distance funcion 'manhattan','euclidean','cosine',
%         'minkowski' and quasi distance 'minp_manhartan'
%   mode - an operation mode of distance function 'RvR' - row versus row
%         (default), and 'RvM' - reserved (not yet implemented) for future
%         use row versus matrix as it is for original Matlab pdist function
%
% Copyright 2011 - P. Skurowski
% Author : P. Skurowski
% Place: Institute of informatics, Silesian Univ. of  Technology
% version 1.0
% See also PAIR_DIST_SEQ, PAIR_DIST_PAR, PDIST

if nargin < 2
    mode='RvR';
    disp(strcat('Default mode ',char(32),char(39),'RvR',char(39), ' - Row versus Row mode'))
end

if strcmp(mode,'RvR')
    disp('RvR')
    switch fun
        case 'manhattan'
            handler = @(x,y)sum( abs(x-y) );  % manhattan (minkowski 1)
        case 'euclidean'
            handler = @(x,y)sqrt( sum( (x-y).^2 )); % euclid (minkowski 2)
        case 'cosine'
            handler = @(x,y)1-x*y'/sqrt(x*x')/sqrt(y*y'); % cosine distance
        case 'minp_manhattan' %minimal permutation manhattan dist
            handler = @(x,y)min(sum(abs(bsxfun(@minus,x(perms(1:5)),y)),2));
            %handler = @(x,y)min(sum(abs(y(perms(1:5))- ...
            %    x(ones(factorial(length(x)), 1),:)),2));
        case 'minkowski'
            handler = @(x,y,p)( sum( abs(x-y).^p )).^(1/p); % Minkowski
        otherwise
            error('Unknown method.');
    end
elseif strcmp(mode,'RvM')
    disp('RvM')
    disp(strcat(char(39),'RvM',char(39), ' - Row versus Matrix mode - reserved for future releases'))
    return
end

Contact us