function [z] = brain_partitioning(isbrain,  y)
%isbrain = the brain mask
%y = a list of the maximum correlation  neighbors. 


numslices = size(isbrain,3);
n = size(isbrain,1);
tc = size(isbrain,4);

%separate into segments
z = ones(n, n,numslices)*10000;
k=1;
for i=1:size(y,1)
    if (z(y(i,1),y(i,2),y(i,3))==10000 & z(y(i,4),y(i,5),y(i,6))==10000) %still haven't defined this area
        z(y(i,1),y(i,2),y(i,3))=k;
        z(y(i,4),y(i,5),y(i,6))=k;
        k = k+1;
    else
        curmin = min(z(y(i,1),y(i,2),y(i,3)),z(y(i,4),y(i,5),y(i,6)));
        curmax = max(z(y(i,1),y(i,2),y(i,3)),z(y(i,4),y(i,5),y(i,6)));
        z(y(i,1),y(i,2),y(i,3)) = curmin;
        z(y(i,4),y(i,5),y(i,6)) = curmin;
        if curmax< 10000
            z(z==curmax) = curmin;
        end
    end
end
z(z==10000) = 0;

newindex = 1;
temp = z; 
for i=1:max(max(max(z)))
    if (~isempty(z(z==i)))
        temp(z==i) = newindex;
        newindex = newindex+1;
    end
end
z =temp;

temp = z(z>0);
if ~isempty(temp)
    a=tabulate(temp);
    [mean(a(:,2)) min(a(:,2)) max(a(:,2)) median(a(:,2)) std(a(:,2))]
end