 
N = 40; % Number of subjects in the dataset
S = 10; % Number of pictures per subject

l= 112; % dimensions of each picture
w = 92;

trainingDataSet = zeros(l,w,(N)*(S-1)); % Saving space in memory for the training set
testDataSet = zeros(l,w,N); % Saving space in memory for the test set

% This is a loop that charges all the images in the test and training sets 
for i = 1:N*S
   subject = fix((i-1)/S)+1;
   picture = mod(i,S);
   if picture==0
       picture =10;
       str = ['orl_dataset/s',num2str(subject),'/',num2str(picture),'.pgm'];
       testDataSet(:,:,subject) = double(imread(str));
   else
       str = ['orl_dataset/s',num2str(subject),'/',num2str(picture),'.pgm'];
        trainingDataSet(:,:,(subject-1)*9+picture) = double(imread(str));
    end
end
   
    


%2) Computing vectorized face and covariance matrix. 
vectorizedTrainingSet; %Reshape the training set into a matrix of apppriate dimensions

vectorizedMeanFace; % Compute the mean of vectorizedTrainingSet. Make sure that it has the correct dimension
Sigma; %Compute the covariane matrix make sure that it has the correct dimension
[V,D];%  V are the eigenvectors and D the eigenvalues

%3) Compute the principal component decomposition for all the training dataset
trainingPCA; % Compute the PC of all the images in the training DataSet;

vectorizedTestSet %Reshape the training set into a matrix of appropriate dimension 
testPCA; % Compute the PC of all the images in the test set


%4) Classifier
accurate_predictions = 0;
for test_subject = 1:40
    
    min_distance = Inf;
    index=0;
    for training_image=1:360 
        distance;     % Compute the distance between the PC representation in the test set with index test_subject 
                        % with the image in the test set with PC representation with index training_image 
        if distance < min_distance  % This Condition takes care of keeping track of the smallest of the distances
            min_distance = distance;
            index = training_image;  % And it saves the index of the closest image 
        end


    end

    predicted_subject = fix((index-1)/(S-1))+1; % Explain this line

    if predicted_subject == test_subject 
       %If the prediction is correct increase the count of accurate predictions
    end
end
    accuracy; % Compute the accuracy of the clasifier.



