% compound subspace method (CSM.m) 

clear all;
close all;

rd=20; % dimensionality of dictionaly subspace

nclass=30; % total number of classes
load('doubutsu.mat'); % load training and test data

ntrai=size(trai,2);
ntest=size(test,2);
% eigenvalue decomposition for each training data set
for ii = 1 : ntrai
  [U,eig_val]=EVD(trai(ii).X);
  trai(ii).U=U;
end
% compound subspace method
% confusion matrix
CONF=zeros(nclass);
w=sqrt([rd:-1:1]'); % linear weight
tic
for query = 1 : ntest
  sim=zeros(nclass,1);
  X=test(query).X;
  [dim num]=size(X);
  for jj = 1 : nclass
    U=trai(jj).U(:,1:rd);
    for ii = 1 : num
      S=norm(w.*(U'*X(:,ii)));
      sim(jj)=sim(jj)+S;
    end
  end
  [value index]=max(sim);
  CONF(test(query).label,index)=CONF(test(query).label,index)+1;
end
finish=toc;
accuracy=(sum(diag(CONF))./ntest).*100;
fprintf(2,'rd=%d accuracy=%3.1f %%\n',rd,accuracy);
fprintf(2,'classification time per test dataset [s]: %f\n',finish./ntest);
