function [corlist, maxcorlist, m1, m2, m3] = brain_neighbcor_all(brain, isbrain,is2Dcon)

%input: brain nxnxtc, isslice indicator
%output: sum(sum(isbrain))*26 correlation list.
%is2Dcon: 0 if use whole brain connectivity, 1 if use only slice
%connectivity. 

numslices = size(brain,3);
n = size(brain,2);
m = sum(sum(sum(isbrain)));

if is2Dcon==1
    %connectivity 8
    e = [ 1 0 0 ;  0 -1 0;  -1 0 0;   0 1 0 ;  1 1 0 ; -1 1 0 ;  1 -1 0; -1 -1 0];
else 
    %connectivity 26
    e =  [ 1 0 0 ;  0 -1 0;  -1 0 0;   0 1 0;  0 0 -1; 0 0 1; 1 1 0; -1 1 0;  1 -1 0; -1 -1 0; 1 0 1 ;  0 -1 1;  -1 0 1;   0 1 1; 1 0 -1 ;  0 -1 -1;  -1 0 -1;   0 1 -1; 1 1 1; -1 1 1;  1 -1 1; -1 -1 1; 1 1 -1; -1 1 -1;  1 -1 -1; -1 -1 -1 ];
end    
corlist = zeros(m*26,8);
l=1;
for h=1:numslices
    for i=1:n
        for j=1:n
            if ~(isbrain(i,j,h))
                continue
            end
            temp = zeros(1,8);
            for k=1:length(e)
                c=e(k,:)+[i j h] ;
                if (c(1) > n  | c(1) <1 | c(2)<1 | c(2)> n | c(3)<1 | c(3)>numslices)
                    continue
                end
                 if ~(isbrain(c(1),c(2), c(3)))
                    continue
                 end
                Y =squeeze(brain(i,j,h,:));
                nY =squeeze(brain(c(1),c(2),c(3),:));
                mycor = corrcoef(Y,nY);                
                mycor = mycor(1,2);
                isd=0;
                if k>6 
                    isd=1;
                end
                if k>18
                    isd=2;
                end
                temp = [i j h c(1) c(2) c(3) mycor isd];
               if temp(1)==0
                continue
              end
            corlist(l,:) = temp;
            l=l+1;          
         end %k
        end   %j     
    end %i
end %h

corlist = corlist(1:(l-1),:);


 index1=find(corlist(:,8)==0);
 index2 = find(corlist(:,8)==1);
 index3 = find(corlist(:,8)==2);
 m1 = median(corlist(index1,7));
 m2 = median(corlist(index2,7));
m3 = median(corlist(index3,7));


 maxcorlist = zeros(m,8);
l=1;
for h=1:numslices
    for i=1:n
        for j=1:n
            if ~(isbrain(i,j,h))
                continue
            end
            temp = zeros(1,8);
            maxcor = -1; 
            for k=1:length(e)
                c=e(k,:)+[i j h] ;
                if (c(1) > n  | c(1) <1 | c(2)<1 | c(2)> n | c(3)<1 | c(3)>numslices)
                    continue
                end
                 if ~(isbrain(c(1),c(2), c(3)))
                    continue
                 end
                Y =squeeze(brain(i,j,h,:));
                nY =squeeze(brain(c(1),c(2),c(3),:));
                mycor = corrcoef(Y,nY);
                mycor = mycor(1,2);
                isd=0;
                if is2Dcon==1
                    if k>4
                        mycor = m1*sqrt(mycor/m2);
                        isd=1;
                    end
                else
                    if k>6 
                       if k>18
                           mycor = m1*sqrt(mycor/m3); 
                           isd = 2; 
                       else
                           mycor = m1*sqrt(mycor/m2);
                        isd=1;
                    end
                    end
                end
                if mycor>maxcor
                    maxcor = mycor;
                    temp = [i j h c(1) c(2) c(3) mycor isd];
                end
            end %k
            if temp(1)==0
                continue
            end
            maxcorlist(l,:) = temp;
            l=l+1;          
        end   %j     
    end %i
end %h

maxcorlist = maxcorlist(1:(l-1),:);
    
     
     