function [candidatesAffinity] = CalcMissPHsAffinityByAttributes(candidates, averageTop, data, attData, last_known_node, addMissingAtt, threshold)
    % sigal 29.10.13 
    % based on CalcAttributesAffinity_S5

    n = size(data,1);
    numAtt = size(attData,2);
    firstPH = last_known_node+1;
    numPHs = size(data,1) - firstPH +1;
 
    if firstPH > n
        LogMsg(sprintf('*** ERROR: CalcPHsAffinityByAttributes firstPH %d >size %d.',firstPH, n));
        return
    end
    
    
    if addMissingAtt > 0 && last_known_node < n
        attData = AddMissingNodesAttrFromNeighborsNeighbors(data, attData, last_known_node, addMissingAtt);
    end
    
    candidatesInd = find(candidates>0);
    totalInd = [candidatesInd,firstPH:size(data,1)];
    attData = attData(totalInd,:);
    totalNum = size(attData,1);
    
    count = sum(attData,2);
    nnz_n = nnz(count);
    nnz_p = 100*nnz_n/totalNum;
    LogMsg(sprintf('PHsAttAffinity count  (%d, %.3f) - nnz=%d, %7.5f%%',numAtt,threshold,nnz_n, nnz_p));

    % full calculation  
    common = attData*attData';
    affinity = zeros(totalNum,totalNum);
    for i=1:totalNum
        for j=1:totalNum
            val = common(i,j);
            total = count(i)+count(j)-val;
            if total > 0 && val/total > threshold
               affinity(i,j) = val/total;
            end
        end
    end

    nnz_n = nnz(affinity);
    nnz_p = 100*nnz_n/(totalNum*totalNum);
    LogMsg(sprintf('PHsAttAffinity totalC (%d, %.3f) - nnz=%d, %7.5f%%',numAtt,threshold,nnz_n,nnz_p));

    clear('common');
    clear('count');

    candidatesNum = nnz(candidatesInd);
    candidatesAffinity = zeros(1, size(candidates,2));
    
    for i=1:candidatesNum
        sortedA = sort(affinity(i,:),'descend');
        val = sum(sortedA(1:averageTop),2)/averageTop;
        candidatesAffinity(1,candidatesInd(1,i)) = val;
    end
    
    clear('affinity');
end %function
