

numCluster = counta; 
tssAll = zeros(nimage, numCluster); 
dataWeightAll = zeros(nimage,numCluster);
symAllcluster = cell(1, numCluster); 
AsymAll = cell(nimage, numCluster); 

disp(['initializing k-mean']);
for (c = 1:counta)
    j = selecta(c); 
    for (i = 1:nimage)
       tssAll(i, c) = dataWeightAll0(i, j);  
    end
end
      


% MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mex Clearn1.c;   % compile C code

numIt = 3; 
for (it = 1 : numIt)
disp(['assign categories in iteration ' num2str(it)]);
for (i = 1 : nimage)
      maxd = max(tssAll(i, :)); 
      if (maxd>0.)
        for (c = 1 : numCluster) 
         dataWeightAll(i, c) = (tssAll(i, c)==maxd)*1.; 
        end
     end
end

disp(['find the means of categories in iteration ' num2str(it)]);
for (c = 1:numCluster)
    tic
    dataWeight = dataWeightAll(:, c) + 0.; 
    Clearn(nimage, norient, allfiltered, allfilteredback, C, h, sx, sy, Mi, Mx, My, Mm, allsymbol(1, :), sym, Asym, Lrange, Orange, sub, Totalsketch, Upperbound, tss, SHUTUP, binnum, dataWeight);
    disp(['   mex-C learning time for cluster ' num2str(c) ' takes ' num2str(toc) ' seconds']);
    tssAll(:, c) = tss + 0.; 
    symAllcluster{c} = sym + 0.; 
    for (i = 1 : nimage)
        AsymAll{i, c} = Asym{i} + 0.; 
    end
end


if (it==numIt)
 disp(['Displaying the clusters']);
 for (cl = 1:numCluster)    
    K = nimage;
    tss = tssAll(:, cl); 
    [tsssort, ind] = sort(tss); 
    tt = 0; 
    for (i = 1 : K)
       this = ind(nimage-i+1); 
       if (dataWeightAll(this, cl)==max(dataWeightAll(this, :)))
          tt = tt + 1; 
          Iactu{tt} = I{this}; 
          Symactu{tt} = -(AsymAll{this, cl}); 
       end
    end

K = tt; bx =0; by = 0; cc = 3;
col = 6; row = floor(K/col)+1;
Iout = zeros(row*sx+(row-1)*(2*bx+cc), 2*col*sy+(col-1)*(2*by+cc))+255; 
towrite = -(symAllcluster{cl}); 
towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
Iout(1:sx, 1:sy) = towrite;
i = -1;  
for (r = 1 : row)
    for (c = 1 : col)
        i = i+1; 
        if ((i>=1)&&(i<=K))
            towrite = Iactu{i}; 
            towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
            Iout((r-1)*sx+1+(r-1)*(2*bx+cc): r*sx+(r-1)*(2*bx+cc), (c-1)*2*sy+1+(c-1)*(2*by+cc): (c-1)*2*sy+sy+(c-1)*(2*by+cc)) = towrite; 
            towrite =  Symactu{i}; 
            towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
            Iout((r-1)*sx+1+(r-1)*(2*bx+cc) : r*sx+(r-1)*(2*bx+cc), (c-1)*2*sy+sy+(c-1)*(2*by+cc)+1 : (c-1)*2*sy+sy+(c-1)*(2*by+cc)+sy) =towrite;  
        end
    end
end
for (r = 1 : row-1)
  Iout(r*sx+(r-1)*(2*bx+cc)+bx+1:r*sx+(r-1)*(2*bx+cc)+bx+cc, :) = 0; 
end
for (c = 1 : col-1)
  Iout(:, c*(sy*2)+(c-1)*(2*by+cc)+by+1:c*(sy*2)+(c-1)*(2*by+cc)+by+cc) = 0; 
end
outepsgif(Iout, ['ex9_Kmean_cluster' num2str(cl)]); 
 end

 for (c = 1:numCluster)   
     outepsgif(-(symAllcluster{c}), ['ex9_Kmean_clusterTemplates' num2str(c)]); 
 end
end
end



