MATLAB Answers

0

Error using - Matrix dimensions must agree. Error in Eigenfaces (line 14) t=double(z(i,:))-m;

Asked by Bereket Ayalew on 19 May 2019 at 22:22
Latest activity Answered by Geoff Hayes
on 20 May 2019 at 3:44
function [ ] = Eigenfaces( )
z=[];
ta=[];
for ii=1:100
str=int2str(ii);
str=strcat(str,'.jpg');
im=imread(str);
im=rgb2gray(im);
im=imresize(im,[256 256]);
temp=reshape(im,65536,1);
z=[z temp];m=mean(z,2);
end
for i=1:size(z,2)
t=double(z(i,:))-m;
ta=[ta t];
end
R=ta'*ta;
[v,d]=eig(R);
L_eig_vec=[];
for i=1:size(v,2)
if(d(i,i)>1)
L_eig_vec=[ L_eig_vec ; v(:,i) ];
end
end
Eigenfaces = ta*L_eig_vec;
disp('eig finished');
projectedimages = [];
Train_Number = size(ta,2);
for i = 1 : Train_Number
temp = Eigenfaces'*ta(:,i); %he
projectedimages = [projectedimages temp];
end
target=eye(Train_Number);
save latest.mat
end

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Sulaymon Eshkabilov on 20 May 2019 at 3:23
Edited by Sulaymon Eshkabilov on 20 May 2019 at 3:24

Hi,
Here is the corrected answer for MATLAB 2013 version:
function [ ] = Eigenfaces( )
z=[];
ta=[];
for ii=1:100
str=int2str(ii);
str=strcat(str,'.jpg');
im=imread(str);
im=rgb2gray(im);
im=imresize(im,[256 256]);
temp=reshape(im,65536,1);
z=[z temp]; m=mean(z,2);
end
for i=1:size(z,2)
t=double(z(:,i))-m;
ta=[ta t];
end
R=ta'*ta;
[v,d]=eig(R, R, 'chol');
L_eig_vec=[];
for i=1:size(v,2)
if(d(i,i)>1)
L_eig_vec=[ L_eig_vec ; v(i,i) ];
end
end
Eigenfaces = ta*L_eig_vec;
disp('eig finished');
projectedimages = [];
Train_Number = size(ta,2);
for i = 1 : Train_Number
temp = Eigenfaces'*ta(:,i);
projectedimages = [projectedimages temp];
end
target=eye(Train_Number);
save latest.mat
end
Good luck.

  0 Comments

Sign in to comment.


Answer by Geoff Hayes
on 20 May 2019 at 3:44

Bereket - the error message is telling you that the matrices upon which you are executing the difference (of), are of different dimensions. When subtracting one matrix from an another, both must have the same number of rows and columns. In the case of your code
t=double(z(i,:))-m;
what are the dimensions of z(i,:) compared with the dimensions of m? Or are you expecting m to be a scalar?
Also, you have a line of code
Eigenfaces = ta*L_eig_vec;
where Eignefaces is the name of your function. Is this intentional? Or should this just be local variable named differently from the function?

  0 Comments

Sign in to comment.