function [FinalClusters, dataMemberShipAll]  = clustering_OrigEM_HOG(k, fv, maxIter, resultPath, imgFiles, winSize)

   
    
[numEx, dim] = size(fv);


differenceAll = zeros(numEx, k);
dataMemberShipAll = rand(numEx,k);
dataMemberShipAll = dataMemberShipAll./repmat(sum(dataMemberShipAll,2),1,k);

clusters=struct('imageIndex',cell(k,1),'mean',[], 'numMember',[]);


[~,ind]=max(dataMemberShipAll,[],2);  % find max in each row
dataMemberShipAll=zeros(size(dataMemberShipAll));  % set zeros
dataMemberShipAll(sub2ind(size(dataMemberShipAll),1:length(ind),ind'))=1;  % hard classification


for iImg=1:size(dataMemberShipAll,1)
      [~, ind]=max(dataMemberShipAll(iImg,:));
      clusters(ind).imageIndex=[clusters(ind).imageIndex,iImg];
end

ShowAssignmentHoG(0,clusters,resultPath,imgFiles, winSize);
    
for it = 1 : maxIter    
      
    disp(['M-step of iteration ' num2str(it)]);
    % initialize the folders and aggregate the rHat map;
    for c = 1:k

        numMember = sum(dataMemberShipAll(:,c));
        clusters(c).numMember=numMember;
        clusters(c).mean = zeros(1,dim);
    end
    
    for iImg = 1:numEx
        %mapName = fullfile(cachePath,['SUMMAXmap-image' num2str(iImg)]);
        featureVec=fv(iImg,:);
        for iCluster = 1:k
            if dataMemberShipAll(iImg,iCluster)~=0
               
               clusters(iCluster).mean =   clusters(iCluster).mean +  featureVec ./ clusters(iCluster).numMember;
                
            end
        end
    end

    for iImg = 1:numEx
        
        featureVec=fv(iImg,:);
        for iCluster= 1:k           
            mean=clusters(iCluster).mean;
            differenceAll(iImg, iCluster)= norm(featureVec-mean,2);
        end % iCluster
    end
    
    dataMemberShipAll=zeros(size(dataMemberShipAll));  % set zeros
    [~,ind]=min(differenceAll,[],2);  % find max in each row
    dataMemberShipAll(sub2ind(size(differenceAll),1:length(ind),ind'))=1;  % hard classification
  
    %%%%% generating html
    for iCluster = 1:k
      clusters(iCluster).imageIndex=[];
    end
    for iImg=1:size(dataMemberShipAll,1)
      [~, ind]=max(dataMemberShipAll(iImg,:));
      clusters(ind).imageIndex=[clusters(ind).imageIndex,iImg];
    end
    
    ShowAssignmentHoG(it,clusters,resultPath, imgFiles, winSize);
end


FinalClusters=[];

for iCluster=1:k
  FinalClusters = [FinalClusters; clusters(iCluster).mean];
end

