Home > chronux_1_50 > spikesort > CORE_mindist.m

CORE_mindist   Windows

PURPOSE ^

CORE_MINDIST Core computational routine for MINDIST.

SYNOPSIS ^

function [k,d] = CORE_mindist(x,y)

DESCRIPTION ^

CORE_MINDIST      Core computational routine for MINDIST.
   [K,D] = CORE_MINDIST(X,Y), given an M x P matrix X and an 
   N x P matrix Y, returns the M x 1 vectors K and D such that 
   K(i) = argmin_j ||X(i,:) - Y(j,:)||^2   and
   D(i) = min_j ||X(i,:) - Y(j,:)||.
   (||.|| means the Euclidean 2-norm -- note that the values in
   D are _squared_ distances.)
 
   CONDITIONS
   ----------
   X and Y must be REAL 2-D arrays of type DOUBLE.
   X and Y must have the same number of columns.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [k,d] = CORE_mindist(x,y)
0002 %CORE_MINDIST      Core computational routine for MINDIST.
0003 %   [K,D] = CORE_MINDIST(X,Y), given an M x P matrix X and an
0004 %   N x P matrix Y, returns the M x 1 vectors K and D such that
0005 %   K(i) = argmin_j ||X(i,:) - Y(j,:)||^2   and
0006 %   D(i) = min_j ||X(i,:) - Y(j,:)||.
0007 %   (||.|| means the Euclidean 2-norm -- note that the values in
0008 %   D are _squared_ distances.)
0009 %
0010 %   CONDITIONS
0011 %   ----------
0012 %   X and Y must be REAL 2-D arrays of type DOUBLE.
0013 %   X and Y must have the same number of columns.
0014 
0015 %%%%%%%%%%%%%%%%%%%%%%%%%%% Prep inputs %%%%%%%%%%%%%%%%%%%%%%%%%%
0016 normsqrX = sum(x.^2,2);
0017 normsqrY = sum(y.^2,2);
0018 
0019 [N,P1] = size(x);   % not going to check P1==P2 out of stubbornness --
0020 [M,P2] = size(y);   %   CORE_ functions are described as not doing error checking
0021 
0022 
0023 %%%%%%%%%%%%%%%%%%%%%% Nearest Vector Search %%%%%%%%%%%%%%%%%%%%%
0024 % Warning: May be verry slow ...
0025 k = zeros(N,1);
0026 for i = 1:N
0027     d(i) = Inf;  k(i) = 0;
0028     for j = 1:M
0029         distsq = sum([x(i,:) - y(j,:)].^2);
0030         if (distsq < d(i)), 
0031             d(i) = distsq;  k(i) = j;
0032         end
0033     end
0034 end
0035 
0036 return;
0037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%% TEST CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0039 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0040 % N = 1000;
0041 % X = randn(N,10);
0042 % tic;  [K,D] = CORE_mindist(X,X);
0043 % printf('CORE_mindist took %5.3f sec.', toc);
0044 % if (K ~= [1:N]' | ~all(abs(D)<2*eps)), printf('  ... but did not find the correct answer.');  end
0045

Generated on Mon 09-Oct-2006 00:54:52 by m2html © 2003