Make data
Xcal   = X(1:2:10,:,:);
Ycal   = salt(1:2:10);
Xtest  = X(2:2:10,:,:);
Ytest  = salt(2:2:10);

Center the data across the first mode in order to possible differences in off-sets
CentX  = [1 0 0];
ScalX  = [0 0 0];
[Xcalmean,MeansX,ScalesX]=nprocess(Xcal,CentX,ScalX);

CentY  = [1 0];
ScalY  = [0 0];
[Ycalmean,MeansY,ScalesY]=nprocess(Ycal,CentY,ScalY);

Preprocess the validation data using given mean values
Xtestmean = nprocess(Xtest,CentX,ScalX,MeansX,ScalesX);
Ytestmean = nprocess(Ytest,CentY,ScalY,MeansY,ScalesY);

Calculate an N-PLS model & get model parameters
[Xfactors,Yfactors,Core,B]=npls(Xcalmean,Ycalmean,4);
[T,Wj,Wk] = fac2let(Xfactors);
[U,Q] = fac2let(Yfactors);

Predict the left-out samples
Yn=[];
RMSEPn=[];
for i=1:4
    yp=npred(Xtestmean,i,Xfactors,Yfactors,Core,B);
    yp = yp+MeansY{1};
    Yn=[Yn yp];
    SS= (yp-Ytest)'*(yp-Ytest);
    RMSEPn=[RMSEPn sqrt(SS/5)];
end

Calculate an unfold-PLS model & get model parameters
[Xfactors,Yfactors,Core,B]=npls(reshape(Xcalmean,5,88),Ycalmean,4);
% Exactly the same as above but now the dimensions are changed
% Instead of [5 11 8] it's [5 88 1], i.e., a two-way matrix
[t,w] = fac2let(Xfactors);
[u,q] = fac2let(Yfactors);

Predict the left-out samples
Y=[];
RMSEP=[];
for i=1:4
    yp=npred(reshape(Xtestmean,5,88),i,Xfactors,Yfactors,Core,B);
    yp = yp+MeansY{1};
    Y=[Y yp];
    SS= (yp-Ytest)'*(yp-Ytest);
    RMSEP=[RMSEP sqrt(SS/5)];
end

Compare prediction errors
format bank

disp('             RMSEP')
disp('           LV           N-PLS          PLS')
disp([[1:4]' RMSEPn' RMSEP'])

format

Compare predictions using two components
subplot(1,2,1)
plot(salt(2:2:10),Yn(:,2),'o')
title('N-PLS model','FontWeight','Bold')
xlabel('Reference content')
ylabel('Predictions')
hold on
plot(salt,salt,'r') % Add target line
hold off

subplot(1,2,2)
plot(salt(2:2:10),Y(:,2),'o')
title('PLS model','FontWeight','Bold')
xlabel('Reference content')
ylabel('Predictions')
hold on
plot(salt,salt,'r') % Add target line
hold off