%[u,e_val,u_temp,e_val_temp,m] = pca_rep(x, numvecs, imagerows, imagecols);

function [u,e_val] = pca_rep(x, numvecs, imagerows, imagecols);

% subtracting the mean block from the blocks (normalize)
num = size(x,2);
meanx = mean(x);
for incr=1:num
      z(:,incr) = x(:,incr) - meanx(incr); %for each block vector we substrackt its mean from it
end

A = z;
num_sub_img = size(A,2);


fprintf(1,'Mean subrated from the image, size %d %d\n', size(A));

%Calculating Eigen vectors and eigen values
[e_vec,e_val] = eig(A*A');
fprintf(1,'Eigen vector/values found\n');
size(e_val);

% Sort 
diagonal = diag(e_val);
temp_evec = zeros(size(e_vec,1), numvecs);
[diagonal,index] = sort(diagonal);
index = flipud(index);
for i = 1:numvecs
    temp_eval(i,i) = e_val(index(i),index(i));
    temp_evec(:,i) = e_vec(:,index(i));
end;

e_vec = temp_evec;
e_val = temp_eval;
u= temp_evec;

fprintf(1,'Eigen vectors for A * Atranspose found amd sorted\n');

