-
Notifications
You must be signed in to change notification settings - Fork 10
/
Classify_2DPCAL1.m
48 lines (40 loc) · 1.53 KB
/
Classify_2DPCAL1.m
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
38
39
40
41
42
43
44
45
46
47
48
function acc=Classify_2DPCAL1(x_train,x_test,label_train,label_test)
% 2DPCA-L1
% 2DPCA with L1-norm for simultaneously robust and sparse modelling
% Copyright (C) 2013 Jing Wang
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
d=size(x_train,1);
n_train=length(label_train);
n_test=length(label_test);
W=PCA2DL1(x_train);
nComp=size(W,2);
acc=zeros(nComp,1);
x_train_reserve=zeros(d,nComp,n_train);
for iSub=1:n_train
x_train_reserve(:,:,iSub)=x_train(:,:,iSub)*W;
end
x_test_reserve=zeros(d,nComp,n_test);
for iSub=1:n_test
x_test_reserve(:,:,iSub)=x_test(:,:,iSub)*W;
end
for iComp=1:nComp
x_train=x_train_reserve(:,1:iComp,:);
x_test=x_test_reserve(:,1:iComp,:);
x_train=reshape(x_train,numel(x_train)/n_train,n_train);
x_test=reshape(x_test,numel(x_test)/n_test,n_test);
dxx=pdist2(x_train',x_test');
[~,ix]=min(dxx);
acc(iComp)=mean(label_test==label_train(ix));
end