function [membership,objectives,permmember] = partitionfnd(prox,lincon)



%initializing arrays

n = size(prox,1);
xi = (1.0e+20)*ones(n,n);
nxi = zeros(n,n);
clus = zeros(n,n);
proxpermut = prox(lincon,lincon);


%initial single subset processing and
%homogeneity calculations

for nlb = 1:n
    for nub = nlb:n
        
                 
                subsetsize = (nub - nlb) + 1;              
               
                index = max(max(proxpermut(nlb:nub,nlb:nub)));
                  
     
        
        if (nlb == 1)
            xi(1,nub) = index;
            nxi(1,nub) = 1;
        end
        
        clus(nlb,nub) = index;
    end
end

%processing the recursion

for k = 2:n
    
    for i = (k-1):(n-1)
        for jone = (i+1):n
            
            temp = max([xi(k-1,i),clus(i+1,jone)]);
            
            if(temp < xi(k,jone))
                
                xi(k,jone) = temp;
                nxi(k,jone) = i + 1;
                
            end
        end
    end
end


%this does the membership for a given cluster size (not all subsets of that size)

membership=[];
objectives=[];
for j = 1:n
for k = j:j
    membershipvec=zeros(1,n);
    cluster = 0;

    
    nend = n;
    nk = k;
    objective = xi(nk,nend);
    
          
        
        add=lincon(nxi(nk,nend):nend);
        cluster = cluster+1;
        a = length(add);
        for i=1:a
            membershipvec(add(i)) = cluster;
        end
      
        
      
     while(nxi(nk,nend) ~= 1)
        
        nend = nxi(nk,nend) - 1;
        nk = nk - 1;
                        
        
        add=lincon(nxi(nk,nend):nend);
        cluster = cluster+1;
        a = length(add);
        for i=1:a
            membershipvec(add(i)) = cluster;
        end

        
    end
    cluster = cluster+1;
    
end
membership = [membership; membershipvec];

objectives = [objectives;objective];
end

permmember =  membership(:,lincon);



