I am using the following code to compressed an image for different principal component. But I faced the error Matrix dimensions must agree. Error in imgP (line 9) X = fly-repmat(mn,1,n);. Please help

[fly,map] = imread('85E.png');
fly=double(fly);
whos
image(fly)
colormap(map)
axis off, axis equal
[m,n]=size(fly);
mn = mean(fly,2);
X = fly-repmat(mn,1,n);
Z=1/sqrt(n-1)*X';
covZ=Z'*Z;
[U,S,V] = svd(covZ);
variances=diag(S).*diag(S);
bar(variances,'b')
xlim([0 20])
xlabel('eigenvector number')
ylabel('eigenvalue')

Answers (1)

Your fly is 3 dimensional but you are only expecting it to be 2 dimensional.
If you want to allow fly to be 3 dimensional then change
[m,n]=size(fly);
to
[m, n, ~] = size(fly);

Asked:

on 22 Dec 2015

Answered:

on 22 Dec 2015

Community Treasure Hunt

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

Start Hunting!