%% PLS subspace

function tmpl = PLS_sub(posSample, negSample, opt)

[nrows,ncols,n_pos] = size(posSample);
n_neg = size(negSample, 3);  
n = n_pos+n_neg;
N = nrows*ncols;
% samples
X = zeros(n,N);
temp = reshape(posSample,[N,n_pos]);
X(1:n_pos,:) = temp';
temp = reshape(negSample,[N,n_neg]);
X(n_pos+1:n,:) = temp';

% label
Y = zeros(n,1);
Y(1:n_pos) = ones(n_pos,1);


%
[X,muX,sigmaX] = zscore(X);
[Y,muY,sigmaY] = zscore(Y);

% centering the training data
muX = mean(X,1);
muY = mean(Y,1);
X = X -  ones(n,1)*muX;
Y = Y - ones(n,1)*muY;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[XL,YL,XS,YS,beta,PCTVAR,MSE,stats]  = plsregress(X,Y,opt.projNum); % SIMPLS
tmpl.W = stats.W;          
tmpl.mean =  reshape(muX',nrows,ncols);
tmpl.sigma = sigmaX';

%mean of the positive samples
tmpl.template = tmpl.W'*X(opt.posNum,:)'; %







