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

4 views (last 30 days)
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
load('.mat', 'X')
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!

Answers (1)

KSSV
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?
load('.mat', 'X')
u = mean(X, 2);
[U,S,V] = svd(X);

Products

Community Treasure Hunt

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

Start Hunting!