%first find the instances whose mre is above certain point

messedUpInstances = [];
successfulInstances = [];
pureSucceedingInstances = dataset;   % instances that did not screw even once

% for coc81 uncomment the following
% [row col] = find(mre0>0.8463);
% for i = 1:size(row,1)
%     tempInstance = actualsWithFeatures((row(i)-1)*size(dataset,1) + col(i),:);
%     messedUpInstances = [messedUpInstances;tempInstance];
% end
% [row col] = find(mre0<=0.8463);
% for i = 1:size(row,1)
%     tempInstance = actualsWithFeatures((row(i)-1)*size(dataset,1) + col(i),:);
%     successfulInstances = [successfulInstances;tempInstance];
% end

% for nasa93 uncomment the line below
[row col] = find(mre0>0.7693);
for i = 1:size(row,1)
    tempInstance = actualsWithFeatures((row(i)-1)*size(dataset,1) + col(i),:);
    messedUpInstances = [messedUpInstances;tempInstance];
end
[row col] = find(mre0<=0.7693);
for i = 1:size(row,1)
    tempInstance = actualsWithFeatures((row(i)-1)*size(dataset,1) + col(i),:);
    successfulInstances = [successfulInstances;tempInstance];
end

% for desharnais uncomment the following
% [row col] = find(mre0>0.1381);
% for i = 1:size(row,1)
%     tempInstance = actualsWithFeatures((row(i)-1)*size(dataset,1) + col(i),:);
%     messedUpInstances = [messedUpInstances;tempInstance];
% end
% [row col] = find(mre0<=0.1381);
% for i = 1:size(row,1)
%     tempInstance = actualsWithFeatures((row(i)-1)*size(dataset,1) + col(i),:);
%     successfulInstances = [successfulInstances;tempInstance];
% end

% now find which instances they were in the actual dataset
actualMessedUpInstances = [];
messUpCounts = [];
scaleFactorForMessUpCounts = size(actualsWithFeatures,1);
while size(messedUpInstances) > 0
    
    % keep how many times the instance messed up
    messUpCounter = 0;
    initialCount = 0;
    finalCount = 0;
    
    % get the effort value to be found
    tempInstance = messedUpInstances(1,:);
    
    % keep the initial count before deleting messed up instance
    initialCount = size(messedUpInstances,1);
    
    % delete the copies of it from the messed up values
    % find in the dataset
    head = 1;                           % keeps track of the head as deletion
    tail = size(messedUpInstances,1);    % keeps track of the tail as deletion
    while 1==1
        if sum(messedUpInstances(head,:) - tempInstance) == 0
            messedUpInstances(head,:) = [];
            tail = tail -1;
        else
            head = head + 1;
        end
        
        % when we processed them all
        if head > tail
           break; 
        end
    end
    % now get the final count and calculate how many times the guy messed
    % up
    finalCount = size(messedUpInstances,1);
    messUpCounter = initialCount - finalCount;
    
    % add the count to the last of the temp instance
    messUpCounts = [messUpCounts;messUpCounter];
    
    % find in the dataset
    for datasetCounter = 1:size(dataset,1)
        if sum(dataset(datasetCounter,:) - tempInstance) == 0
            pureSucceedingInstances(datasetCounter,:) = -1;
            actualMessedUpInstances = [actualMessedUpInstances;tempInstance];
        end
    end
    
end

pureSucceedingInstances(find(pureSucceedingInstances(:,size(dataset,2)) < 0),:)= [];

messUpPerc = messUpCounts*100 / scaleFactorForMessUpCounts;
actualMessedUpInstances = [actualMessedUpInstances messUpCounts messUpPerc];
pureSucceedingInstances = [pureSucceedingInstances zeros(size(pureSucceedingInstances,1),1) zeros(size(pureSucceedingInstances,1),1)];









%%%%%%%%%%%%%%%%%% do the same as above to the succeeding guys
actualSuccessfulInstances = [];
successCounts = [];
scaleFactorForSuccessCounts = size(actualsWithFeatures,1);
while size(successfulInstances) > 0
    
    % keep how many times the instance succeeded
    successCounter = 0;
    initialCount = 0;
    finalCount = 0;
    
    % get the effort value to be found
    tempInstance = successfulInstances(1,:);
    
    % keep the initial count before deleting copies successful instances instance
    initialCount = size(successfulInstances,1);
    
    % delete the copies of it from the messed up values
    % find in the dataset
    head = 1;                           % keeps track of the head as deletion
    tail = size(successfulInstances,1);    % keeps track of the tail as deletion
    while 1==1
        if sum(successfulInstances(head,:) - tempInstance) == 0
            successfulInstances(head,:) = [];
            tail = tail -1;
        else
            head = head + 1;
        end
        
        % when we processed them all
        if head > tail
           break; 
        end
    end
    % now get the final count and calculate how many times the guy
    % succeeded
    finalCount = size(successfulInstances,1);
    successCounter = initialCount - finalCount;
    
    % add the count to the last of the temp instance
    successCounts = [successCounts;successCounter];
    
    % find in the dataset
    for datasetCounter = 1:size(dataset,1)
        if sum(dataset(datasetCounter,:) - tempInstance) == 0
            actualSuccessfulInstances = [actualSuccessfulInstances;tempInstance];
        end
    end
end

successPerc = successCounts*100 / scaleFactorForSuccessCounts;
actualSuccessfulInstances = [actualSuccessfulInstances successCounts successPerc];

