function [affinity] = CalcPHsAffinityByImages(data, imagesData, lastKnownNode, missingNodesMap, missProb, simType, simProbDiff)
    % sigal 23.1.14

    profileFactor = 1000;
    profilePrefix = 100;
    firstPHsRow = 2;
%    simProb = 0.2; %%0.2;
%    misProb = 0; %0.2; %%no images
    
    n = size(data,1);
    firstPH = lastKnownNode+1;
    
    if firstPH > n
        LogMsg(sprintf('*** ERROR: CalcPHsAffinityByImages firstPH %d >size %d.',firstPH, n));
        return
    end
    
    if missProb < 0 || missProb > 0.8
        LogMsg(sprintf('*** ERROR: CalcPHsAffinityByImages invalid parameter (missProbb=%.2f).',missProb));
        return
    end
    
    if simType ~= 0 && (simProbDiff > 0.5 || simProbDiff < 0)
        LogMsg(sprintf('*** ERROR: CalcPHsAffinityByImages invalid parameter (simType=%d, simProbDiff=%.2f).',simType, simProbDiff));
        return
    end
    
    numPHs = size(data,1) - firstPH +1;
    phsImages = zeros(numPHs,1);
    
    for j=1:size(missingNodesMap,2)
        for i=firstPHsRow+1:size(missingNodesMap,1)
            ph = missingNodesMap(i,j);
            if ph ~= 0
                currentPH = ph - lastKnownNode;
                %Sigal - pid = uid*100+p_index (starting from thrid row)
                %avigail - uid.Value*100000 + unameId*1000 +  index
                currentImage = (profilePrefix+missingNodesMap(2,j))*profileFactor+(i-firstPHsRow); 
                phsImages(currentPH) = currentImage;
            end
        end
    end

    maxExpImages = max(phsImages,1);
    maxImagesData = size(imagesData,1);
    if simType == 0 &&  maxImagesData < maxExpImages
        LogMsg(sprintf('*** ERROR: CalcPHsAffinityByImages invalid parameter (simType=%d, maxExpImages=%d>maxImagesData=%d).',simType, maxExpImages, maxImagesData));
        return
    end
    
    
    affinity = zeros(numPHs,numPHs);
    
    for i=1:numPHs
        for j=1:i
            if i==j
                affinity(i,j) = 1;
            else
                % get pictures indexes
                pic1 = phsImages(i);
                pic2 = phsImages(j);
                if pic1>pic2%% swap, make sure pic2>pic1
                    temp = pic2;
                    pic2 = pic1;
                    pic1 = temp;
                end
                
                % get affinity
                if simType == 0 % use real data
                    if size(imagesData,1) >= pic1 && rand(1) >= missProb
                        affinity(i,j) = imagesData(pic1, pic2);
                        affinity(j,i) = affinity(i,j);
                    end
                else % use random data
                    if simType == 1 % 1=rand(uniform distribution) between (0,1)
                        randVal = rand(1);
                    else % 2=randn(normal distribution): mean=0.5, sigma=0.25, 95% between (0,1)
                        randVal = 0.5+randn(1)*0.25; 
                    end
                    if (pic2-pic1)<profileFactor    % i.e. same profile
                        val = randVal*(1-simProbDiff)+simProbDiff;
                    else
                        val = randVal*(1-simProbDiff);
                    end
                    if rand(1) >= missProb
                        affinity(i,j) = val;
                        affinity(j,i) = affinity(i,j);
                    end
                end
            end
        end
    end

    nnz_n = nnz(affinity);
    nnz_p = 100*nnz_n/(numPHs*numPHs);
    LogMsg(sprintf('PHsImgAffinity - nnz=%d, %7.5f%%',nnz_n,nnz_p));
    
end %function
