function [sigmaData] = uniqueFlagFun(sigmaData)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%   Matching partners in sigmaData   %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\n\nMatching partners in sigmaData\n')

out.partnerCount        = 0;
out.noPartnerCount      = 0;
indxji                  = 0;
deleteScreenMsg         = '';

for j = 1:length(sigmaData)
     
   if (mod(j,50) == 0) || j == length(sigmaData) || j == 1
    screenMsg = sprintf(['\n', num2str(length(sigmaData) + 1 - j), ' sigmaData entries remaining to match to partner.\n']);
    fprintf([deleteScreenMsg, screenMsg])
    deleteScreenMsg = repmat(sprintf('\b'), 1, length(screenMsg));
    
    if j == length(sigmaData)
        fprintf(deleteScreenMsg)
    end
    
   end
    
    indxji = [];
    
    for i = 1:length(sigmaData)
        
        if sigmaData(i).numcode.ASSi    == sigmaData(j).numcode.ASSj ...  
        && sigmaData(i).numcode.ATOMi   == sigmaData(j).numcode.ATOMj...
        && sigmaData(i).numcode.ASSj    == sigmaData(j).numcode.ASSi ...
        && sigmaData(i).numcode.ATOMj   == sigmaData(j).numcode.ATOMi
        
        indxji = i;
        end
    end
    
    if ~isempty(indxji)                 %Partner exists
        
        sigmaData(j).indxji             =   indxji;
        
    elseif isempty(indxji)              %Partner does not exist
        
        sigmaData(j).indxji             =   [];
        
    end
end

for j = 1:length(sigmaData)    
    
   
    if isempty(sigmaData(j).indxji) == 1                             %only entry 
     
        sigmaData(j).unique = 1;
                
    elseif ~isempty(sigmaData(j).indxji) && j < sigmaData(j).indxji  %the first one of the double in the series 
        
        sigmaData(j).unique = 2;
                            
    elseif ~isempty(sigmaData(j).indxji) && j > sigmaData(j).indxji  %the second one of a double in the series
      
        sigmaData(j).unique = 0;
    
    end
end


for i = 1:length(sigmaData)
    if ~isempty(sigmaData(i).indxji)
        sigmaData(i).sigmaji                = sigmaData(sigmaData(i).indxji).buildups.sigmaij;
% % %         sigmaData(i).sigmaCorrD2Oji         = sigmaData(sigmaData(i).indxji).sigmaCorrD2Oij;
        sigmaData(i).Izeroji                = sigmaData(sigmaData(i).indxji).buildups.Izero;
    end
end

% % indices.sigmaData.unique1   = find(ismember([sigmaData.unique], 1));
% % indices.sigmaData.unique2   = find(ismember([sigmaData.unique], 2));
% % indices.sigmaData.unique0   = find(ismember([sigmaData.unique], 0));
% % indices.sigmaData.unique12  = find(ismember([sigmaData.unique], [1 2]));
% % indices.sigmaData.unique02  = find(ismember([sigmaData.unique], [0 2]));

out.partnerCount            = length(find(ismember([sigmaData.unique], [0 2])));
out.noPartnerCount          = length(find(ismember([sigmaData.unique], 1)));

%diary on

fprintf([   '\n\nMatched ', num2str(out.partnerCount), ' sigmaData entries to their partner.',...
            '\n', num2str(out.noPartnerCount), ' sigmaData entries have no partner.\n'])
%diary off

% % % fprintf([   '\nsigmaData.unique = 1: No partner.',...
% % %             '\nsigmaData.unique = 2: Partner exists, the first one of the double in the series.',...
% % %             '\nsigmaData.unique = 0: Partner exists, the second one of the double in the series.\n'])

clearvars screenMsg deleteScreenMsg indxji i j

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%