pcares
is doing its job. If you read the documentation, you call the function this way:
[RESIDUALS,RECONSTRUCTED] = pcares(X,NDIM);
RESIDUALS
returns the residuals for each data point by retaining the first NDIM
dimensions of your data and RECONSTRUCTED
is the reconstructed data using the first NDIM
principal components.
If you want the actual projection vectors, you need to use pca
instead. You'd call it this way:
[coeff,score] = pca(x);
In fact, this is what pcares
does under the hood but it also reconstructs the data for you using the above outputs. coeff
returns the principal coefficients for your data while score
returns the actual projection vectors themselves. score
is such that each column is a single projection vector. It should be noted that these are ordered with respect to dominance as you'd expect with PCA... and so the first column is the most dominant direction, second column second dominant direction, etc.
Once you call the above, you simply index into coeff
and score
to retain whatever components you want. In your case, you just want the first component, and so do this:
c = coeff(1);
s = score(:,1);
If you want to reconstruct the data given your projection vectors, referring to the second last line of code, it's simply:
[coeff,score] = pca(x);
n = size(X,1);
ndim = 1; %// For your case
reconstructed = repmat(mean(X,1),n,1) + score(:,1:ndim)*coeff(:,1:ndim)';
The above is basically what pcares
does under the hood.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…