**B.1 PCA by decomposition of the covariance matrix**  % Size of the data matrix X [n,p]=size(X); % Column centering meanX = mean(X); X\_Centred = X - ones(n,1) \* meanX; % Compute variance-covariance matrix % for scores CovM\_scores = cov(X\_Centred); % for variables CovM\_loadings = cov(X\_Centred'); % Compute eigenvalue & eigenvectors by keeping the matrix form [V\_scores,D\_scores] = eig(CovM\_scores); D\_scores = fliplr(D\_scores);D\_scores = flipud(D\_scores); V\_scores = fliplr(V\_scores); [V\_loadings,D\_loadings] = eig(CovM\_loadings); D\_loadings = fliplr(D\_loadings);D\_loadings = flipud(D\_loadings); V\_loadings = fliplr(V\_loadings); % Compute of total variance of the dataset: TotalVP

```
% Compute the percentage of variance explained by each principal
%component
```

```
Pourcentages = {}; 
for i=1:size(D_scores,1) 
 Perrcentages{i,1} = strcat('PC',num2str(i)); 
 Perrcentages{i,2} = num2str(D_scores(i,i)/TotalVP*100); 
end; 
% Compute factorial coordinates (scores) for each sample point on 
% components space 
Coord =[]; 
Coord = X_Cent*V_scores; 
% Compute factorial contributions (loadings) for each sample point 
on 
% components space 
Contrib =[]; 
Contrib = X_Cent'*V_loadings;
```
#### **B.2 PCA by SVD**

36 Analytical Chemistry

2003; p xvii, 569 p.

1989; p xvii, 419 p.

York, 1997.

**B. Matlab codes** 

[n,p]=size(X);

% for scores

% for variables

% Column centering meanX = mean(X);

% Size of the data matrix X

X\_Centred = X - ones(n,1) \* meanX; % Compute variance-covariance matrix

CovM\_scores = cov(X\_Centred);

V\_scores = fliplr(V\_scores);

V\_loadings = fliplr(V\_loadings);

TotalVP = trace(D\_scores);

%component

CovM\_loadings = cov(X\_Centred');

[V\_scores,D\_scores] = eig(CovM\_scores);

[V\_loadings,D\_loadings] = eig(CovM\_loadings);

% Compute of total variance of the dataset: TotalVP

332 p.

Jackson, J. E., *A user's guide to principal components*. Wiley-Interscience: Hoboken, N.J.,

Martens, H.; Næs, T., *Multivariate calibration*. Wiley: Chichester [England] ; New York,

Massart, D. L., *Handbook of chemometrics and qualimetrics*. Elsevier: Amsterdam ; New

Sharaf, M. A.; Illman, D. L.; Kowalski, B. R., *Chemometrics*. Wiley: New York, 1986; p xi,

% Compute eigenvalue & eigenvectors by keeping the matrix form

D\_loadings = fliplr(D\_loadings);D\_loadings = flipud(D\_loadings);

% Compute the percentage of variance explained by each principal

D\_scores = fliplr(D\_scores);D\_scores = flipud(D\_scores);

**B.1 PCA by decomposition of the covariance matrix** 

```
% Size of the data matrix X 
[n,p]=size(X); 
% Column centering 
meanX = mean(X); 
X_Centred = X - ones(n,1) * meanX; 
% Compute scores and loadings simultaneously 
[U SingularValues Loadings] = svd(X_Centred); 
% Scores T 
T = U * SingularValues;
```