function [ meanDistSqr, dist2Lth ] = mDSTLNN( data, L ) % % This function returns the average of the squared distance between each % point and its L'th nearest neighbor. I.E. when L=1 we return the average % of the square of the nearest neighbor distance. % % Written by: % -- % John L. Weatherwax 2005-08-14 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- nSamp = size(data,1); if( L > nSamp ) error( 'L must be less than the number of samples' ); end % Compute the ((distance to Lth)^2) FROM each sample TO each sample: dist2Lth = zeros(1,nSamp); for ii=1:nSamp, xt = data(ii,:); % Compute the squared distance between the datum xt and ALL other datum: dist = zeros(1,nSamp); for jj=1:nSamp, yt = data(jj,:); diff = xt - yt; distSqr(jj) = diff * (diff.'); end [ws,wi] = sort(distSqr); dist2Lth(ii) = ws(L+1); end meanDistSqr = mean( dist2Lth );