



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.


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); d(i) = repmat(Inf, N, 1); 0026 for i = 1:N 0027 for j = 1:M 0028 distsq = sum((x(i,:) - y(j,:)).^2); 0029 if (distsq < d(i)), 0030 d(i) = distsq; k(i) = j; 0031 end 0032 end 0033 end 0034 0035 return; 0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0037 %%%%%%%%%%%%%%%%%%%%%%%%%%%%% TEST CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0038 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0039 % N = 1000; 0040 % X = randn(N,10); 0041 % tic; [K,D] = CORE_mindist(X,X); 0042 % printf('\nCORE_mindist took %5.3f sec.', toc); 0043 % if (K ~= [1:N]' | ~all(abs(D)<2*eps)), printf(' ... but did not find the correct answer.'); end 0044