-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcosmo_classify_nn.m
More file actions
37 lines (31 loc) · 1016 Bytes
/
cosmo_classify_nn.m
File metadata and controls
37 lines (31 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function predicted=cosmo_classify_nn(samples_train, targets_train, samples_test, opt)
% nearest neighbor classifier
%
% predicted=cosmo_classify_nn(samples_train, targets_train, samples_test[, opt])
%
% Inputs
% - samples_train PxR training data for P samples and R features
% - targets_train Px1 training data classes
% - samples_test QxR test data
%- opt (currently ignored)
%
% Output
% - predicted Qx1 predicted data classes for samples_test
%
% NNO Aug 2013
if nargin<4, opt=struct(); end
[ntrain, nfeatures]=size(samples_train);
[ntest, nfeatures_]=size(samples_test);
ntrain_=numel(targets_train);
if nfeatures~=nfeatures_ || ntrain_~=ntrain, error('illegal input size'); end
% allocate space for output
predicted=zeros(ntest,1);
% >>
for k=1:ntest
delta=bsxfun(@minus, samples_train, samples_test(k,:));
dst=sqrt(sum(delta.^2,2));
if numel(dst)~=ntrain, error('wrng'); end
[m, i]=min(dst);
predicted(k)=targets_train(i);
end
% <<