# reshape help doing PCA when all images contained in one .mat file

3 views (last 30 days)
Answered: KSSV on 25 Jan 2023
I'm having issues writing code to reshape vectors correctly
I have a .mat file with variable X ∈ R 3240×200. Each column denotes a face image of size 60 × 54. So 200 faces total. I'm trying to plot an Image of the sample mean face, Images of the first three principal eigenvectors of the covariance matrix, and Images of any random face.
I'm have this code
I = reshape(X(:, 1), 64, 56);
u = mean(X, 2);
svd(X)
eigvecs = svd(X);
I = reshape(eigvecs(:, 1), 64, 56);
The last line gives me
Error using reshape
Number of elements must not change. Use [] as one of the size inputs
to automatically calculate the appropriate size for that dimension.
I can't find an example online wherein all the PCA images are in one singular .mat file. Need to find a way to properly code so the eigvecs are reshaped correctly and matlab will produce the images.
Thanks!

KSSV on 25 Jan 2023
Your X data is of size 3240X20, you cannot reshape its each column to size 64X56..because 64X56=3584~=3240. So you need to use your reshape dimensions wisely. Think of your data properly.
Or, are you expecting this?
u = mean(X, 2);
[U,S,V] = svd(X);

### Categories

Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!