function [label, knnmat] = knnr(sampledata, testdata, k) % KNNR K-nearest neighbor rule for classification % LABEL = KNNR(SAMPLE, INPUT, K) % SAMPLE: data set (last column: desired class) % INPUT: input (feature) matrix, with each row being the values % of a specific feature % K: the "k" in k nearest neighbor % LABEL: KNNR output % % Note that the output label of the SAMPLE is assumed to take % the values from 1 to n, where n is the number of classes. if nargin < 3, k = 1;, end feature_n = size(sampledata,2)-1; sample_input = sampledata(:, 1:feature_n); sample_output = sampledata(:, feature_n+1); out_label = countele(sample_output); % possible output labels class_n = length(out_label); test_n = size(testdata, 1); % no. of test input vectors input = testdata(:, 1:feature_n); % strip out label info, if any % Distance matrix between sample_input and input vector distmat = vecdist(sample_input, input); [junk, index] = sort(distmat); % knnmat(i,j) = class of i-th nearest point of j-th input vector % (The size of knnmat is k times test_n.) knnmat = reshape(sample_output(index(1:k,:)), k, test_n); % label_count(i,j) = count of class-i points within j-th input vector's % neighborhood label_count = zeros(class_n, test_n); for i = 1:test_n, [sorted_element, element_count] = countele(knnmat(:,i)); label_count(sorted_element, i) = element_count; end [junk, label] = max(label_count); label = label';