function [countNN indexCount] = kNN(test, dataBase, k) % Determining the k nearest neighbors of a given point from % a given database using three similarity metrics- counting, lin and re. % Assumption: 1. all points are from the same class % 2. k < number of points in database % Inputs: % 1. point: vector in the same space as the objects in the database % 2. dataBase: matrix (n*m) n= number of objects in the database; m = % dimension of each object (note: point should have same dimension as all % points in the database) % 3. k: number of nearest neighbours % 4. class: distribution that the objects in the database were created from % 5. inputsDistrib: inputs to the above distribution. % Outputs: % 1. countNN: matrix (k*m)- k vectors of dimension m rep the k nearest % neighbours, using counting similarity metric. % 2. linNN: matrix (k*m)- k vectors of dimension m representing the k % nearest neighbours, using lin similarity metric. % 3. indexCount: indices of the kNN using simCounting % 4. indexLin: indices of the kNN using simLin simCount = zeros(size(dataBase,1),2); simlin = zeros(size(dataBase,1),2); simRe = zeros(1, length(dataBase)); for i= 1:size(dataBase,1) [AintersectB AnotB BnotA] = featurescomp(test, dataBase(i,:)); %[probAnotB] = calcProbExclusive('AnotB', AnotB,class, class,... % inputsDistrib, inputsDistrib, 0); %[probBnotA] = calcProbExclusive('BnotA', BnotA,class, class,... % inputsDistrib, inputsDistrib, 0); %[probAintersectB] = calcProbIntersect(AintersectB, class, class, inputsDistrib, inputsDistrib, 0); % hardcoding inputs to similarity metrics for now [simCount(i,:)] = [i simCounting(AintersectB, AnotB, BnotA, 1, 0.5, 0.5)]; %[simlin(i,:)] = [i simLin(probAintersectB, probAnotB, probBnotA)]; end simCount = sortrows(simCount,2); %simCount %simlin = sortrows(simlin,2); indexCount = simCount(size(simCount, 1):-1:size(simCount,1)-k+1,1); %indexLin = simlin(size(simlin, 1):-1:size(simlin,1)-k+1,1); indexLin = indexCount; %disp(simCount); %disp(simlin); j = 1; for i=size(dataBase,1):-1:size(dataBase,1)-k+1 countNN(j,:) = dataBase(simCount(i,1),:); % linNN(j,:) = dataBase(simlin(i,1),:); j= j+1; end linNN = countNN;