www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@constar/private/separation.m

    function h = separation( X )
%SEPARATION   Separation distance of a data set
%   SEPARATION(X) returns the maximum minimum separation between 
%   the given points, i.e., it calculates
%
%           max       min      | x - y | ,
%         x in X   y in X\x
%
%   where the cartesian coordinates of the data points are given 
%   by the rows of X.
%
%   SEPARATION('demo') plots a graph of separation vs dimension for 
%   points uniformly randomly distributed throughout the unit cube.


%  Copyright 2000-2008 The MathWorks, Inc. and Ford Global Technologies, Inc.


if strcmp( X, 'demo' ),
    i_demo;
    h = [];
else
    h = i_separation( X );
end

return

% ---------------------------------------------------------
function h = i_separation( X )

d = size( X, 2 );
T = delaunayn( X );

if ~isempty( T ),
    % use the delaunay to search through an optimal set of pairs
    n = size(T,1);
    TT = zeros( (d+1)*n, 2 ); 
    p = 1:n;
    for i = 1:d, 
        TT(p,:) = T(:,[i,i+1]); 
        p = p + n;
    end, 
    TT(p,:) = T(:,[d+1,1]);
    TT = unique( TT, 'rows' );
    
    R = repmat( Inf, size( X, 1 ), 1 );
    for i=1:size(TT,1),
        t1 = TT(i,1);
        t2 = TT(i,2);
        r = sum( ( X(t1,:) - X(t2,:) ).^2 );
        R(t1) = min( R(t1), r );
        R(t2) = min( R(t2), r );
    end
    R = R(~isinf(R));
    h = sqrt( max( R ) );
    
else
    % no delaunay, have to search all pairs
    h = i_bruteforce( X );
    
end

return

% ---------------------------------------------------------
function h = i_bruteforce( X )

npts = size( X, 1 );
    R = repmat( Inf, npts, 1 );
    for i = 1:npts,
        for j = 1:npts,
            if i ~= j,
                R(i) = min( R(i), sum( ( X(i,:) - X(j,:) ).^2 ) );
            end
        end
    end
    h = sqrt( max( R ) );

return

% ---------------------------------------------------------
function i_demo

D = 10;
n = 15;
N = 100;

a = zeros( D, 1 ); 
h = zeros( n, 1 );
for d = 1:D,
    for i = 1:n,
        h(i) = i_separation( rand( N, d ) );
    end
    a(d) = mean( h );
end

plot( 1:D, a, '.-', 'LineWidth', 2, 'MarkerSize', 24 );
xlabel( 'dimension' );
ylabel( 'separation' );
title( ['Separation vs dimension for ' int2str(N) ...
        ' random points in the unit cube'] );

% EOF