

outResp = zeros(1, nbasis); 


for (di = 1 : nbasis)
    TotalsketchA = TotalsketchAll(di); 
    MiA = MiAll(di, 1:TotalsketchA)+0.; 
    MxA = MxAll(di, 1:TotalsketchA)+0.; MyA = MyAll(di, 1:TotalsketchA)+0.; 
    MmA = MmAll(di, 1:TotalsketchA)+0.; Mm1A = Mm1All(di, 1:TotalsketchA)+0.; 
    outResp(di) = -sum(Mm1All(di, 1:TotalsketchA)); 

    S2map = zeros(sx1, sy1);  % log-likelihood score map for testing images
    disp(['computing S2 map for template ' num2str(di)]);
    tic
    CSum2(norient, M1map, MiA, MxA, MyA, MmA, Mm1A, sub1, TotalsketchA, S2map, sx1, sy1);
    disp(['S2 map takes ' num2str(toc) ' seconds']);
    if (showon>.5)
    outepsgif(S2map, ['ex7_S2_' num2str(di)]); 
    end
    
    disp(['computing M2 map for template ' num2str(di)]);
    tic
    M2map = zeros(sx1, sy1);
    CMax2(S2map, M2map, sub1, sx1, sy1, xRange, yRange);
    disp(['M2 map takes ' num2str(toc) ' seconds']);
    if (showon>.5)
    outepsgif(M2map, ['ex7_M2_' num2str(di)]); 
    end
    
    S2mapAll{di} = S2map+0.; 
    M2mapAll{di} = M2map+0.; 
end

