function [A,B,C]=domergeTurbo(Atemp,Btemp,Ctemp,itrNum,F,fixed_val, allCorrespondences)    
%Ekta Gujral,Ravdeep Pasricha , Vagelis Papalexakis 2017
%Computer Science and Engineering, University of California, Riverside
%Demonstrating the use of the merging algorithm of SambaTen
    i=itrNum;
    fixed_i=fixed_val{1};
    fixed_j=fixed_val{2};
    fixed_k=fixed_val{3};

 if i ==1
        A = Atemp{1}; B = Btemp{1}; C = Ctemp{1};
        
 else
        valA=zeros(F,1);valB=zeros(F,1);valC=zeros(F,1);
        
        
        usedA = [];usedB = []; usedC=[];%here we keep track of the columns we've already used
        for f1 = 1:F
            for f2 = 1:F
                valA(f2) = (Atemp{1}(fixed_i,f1))'* (Atemp{2}(fixed_i,f2));
                valB(f2) = (Btemp{1}(fixed_j,f1))'* (Btemp{2}(fixed_j,f2));
                valC(f2) = (Ctemp{1}(fixed_k,f1))'* (Ctemp{2}(fixed_k,f2));
            end
            
            [junk idx] = sort(valA,'descend');            
            ii = 1;while (find(idx(ii) == usedA))ii = ii +1; end;idx = idx(ii);usedA = [usedA idx];%use the highest non-already used matching column
            mask2 = find(Atemp{2}(:,idx)==0);
            Atemp{2}(mask2,idx) = Atemp{2}(mask2,idx) + Atemp{1}(mask2,f1);%Update ONLY the zero values
            
            allCorrespondences(f1) = idx;
            mask2 = find(Btemp{2}(:,idx)==0);
            Btemp{2}(mask2,idx) = Btemp{2}(mask2,idx) + Btemp{1}(mask2,f1);%Update ONLY the zero values

            mask2 = find(Ctemp{2}(:,idx)==0);
            Ctemp{2}(mask2,idx) = Ctemp{2}(mask2,idx) + Ctemp{1}(mask2,f1);%Update ONLY the zero values
            
            
    end
A=Atemp{2};
B=Btemp{2};
C=Ctemp{2};
 end

end
            
