Error using * Inner matrix dimensions must agree.

function [ ] = Eigenfaces( )
z=[];
ta=[];
for i=1
str=int2str(i);
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

 Accepted Answer

Hi,
There is one very tiny but a really crucial point missed with the index of z(). Here is corrected part:
...
z=[z temp];m=mean(z,2);
end
for i=1:size(z,2)
t=double(z(i,:))-m; % index of z(:,i) has to be z(i,:). That is all
ta=[ta t];
end
Good luck

3 Comments

thank you very much!!!!!!! it starts working normally after these change............ but i do have also another question and that is...... i want to changes many faces in the same directory to train in neural network..... so i use the for loop
for i=1:100
like this to change the images iteratively upto 100..... then it says error in this code line
t=double(z(:,i))-m;
saying "error using '-' sign".... i already rename them like 1 2 3 4 5..... then put them in the correct directory...... but it say also "error in the dimension" on the code line i mentioned above!!! can you help me with this?? i really want them to change to eigen and in the same folder means latest!!!!!!!! thank you for your cooperation!!
Please reformat
Greg
what does reformat mean?? what thing is i reformat??

Sign in to comment.

More Answers (2)

Hi,
I could not find any problem with your corrected for a few .jpg images (created) to process that I've played with.
There was another minor typo (see below given): temp = Eigenfaces'*ta(:,i); %he
The only thing might be the casue I think is that the file extention has to be unique in all of your files, e.g. jpg or jpeg (if mixed then fix them). Noe that your code works with *.jpg only! if you would use *.jpeg then correct the code for *.jpeg extension. I could not generate your problem with my MATLAB. I've tested it with 2017a and 2019a so far. Btw, what version of MATLAB are you using?
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
Good luck.

2 Comments

i use matlab 2013a
'' Error using -
Matrix dimensions must agree.
Error in Eigenfaces (line 14)
t=double(z(i,:))-m; ''.............. still says this
i also checked all the file extension and its correct
Sulaymon Eshkabilov's answer moved here for consistency:
Hi,
The problem is resolved. I have picked up my old pc with MATLAB 2013b and managed to generate your problem. The problem was with the accuracy level and algorith used in eigen-value computation with eig(). In later versions of MATLAB, the most appropriate (accurate) algorithm is chosen automatically, but in older versions this is not the case. Also, a size selection of arrays has been changed in later version - it is rather automatic and well accustomated without a user notice. Anyhow, I have done some small changes and adjusted the code to MATLAB 2013b (2013a - hopefully) with eigenvalue computing algorithm with Choleky decomposition.
Here is the complete code:
function [ ] = Eigenfaces( )
z=[];
ta=[];
for ii=1:4
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. Don't forget Thumbs UP ... :)

Sign in to comment.

Hi,
The problem is resolved. I have picked up my old pc with MATLAB 2013b and managed to generate your problem. The problem was with the accuracy level and algorith used in eigen-value computation with eig(). In later versions of MATLAB, the most appropriate (accurate) algorithm is chosen automatically, but in older versions this is not the case. Also, a size selection of arrays has been changed in later version - it is rather automatic and well accustomated without a user notice. Anyhow, I have done some small changes and adjusted the code to MATLAB 2013b (2013a - hopefully) with eigenvalue computing algorithm with Choleky decomposition.
Here is the complete code:
function [ ] = Eigenfaces( )
z=[];
ta=[];
for ii=1:4
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. Don't forget Thumbs UP ... :)

Community Treasure Hunt

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

Start Hunting!