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

### Bereket Ayalew (view profile)

on 19 May 2019
Latest activity Answered by Geoff Hayes

### Geoff Hayes (view profile)

on 20 May 2019
function [ ] = Eigenfaces( )
z=[];
ta=[];
for ii=1:100
str=int2str(ii);
str=strcat(str,'.jpg');
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

### Sulaymon Eshkabilov (view profile)

on 20 May 2019
Edited by Sulaymon Eshkabilov

### Sulaymon Eshkabilov (view profile)

on 20 May 2019

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=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.

### Geoff Hayes (view profile)

on 20 May 2019

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?