function z=brain_cluster(input_files, isbrain,keep)
%BRAIN_CLUSTER clusters the brain using a 3d correlation based clustering
%method with 26 neighbors. The correlations are based on the concatenated
%data from the input_files. 
%
%Z = BRAIN_CLUSTER(INPUT_FILES, ISBRAIN,KEEP)
%
%INPUT_FILES: is a cell array of names of 4D fMRI image files with multiple  frames
%in ANALYZE (.img)  format.  
%
%ISBRAIN: output of BRAIN_THRESH
%
%KEEP: the time points (frames)  to keep in the analysis from concatenated time series of input files. 
%
%Z: a 3D matrix with positive indices to indicate clusters, 0 is background. 
n = length(input_files);

dm = fmris_read_image(char(input_files(1)));
rbrainLOC = dm.data;


if n>1
    for i=2:n
        dm = fmris_read_image(char(input_files(i)));
        rbrain = dm.data;
        rbrainLOC = cat(4,rbrainLOC,rbrain);
        clear rbrain
    end
end
rbrainLOC = rbrainLOC(:,:,:,keep);



[corlist, maxcorlist, m1,m2,m3] = brain_neighbcor_all(rbrainLOC,isbrain,0); %find adjuster correlatation values and maximum correlation list 3D
z = brain_partitioning(isbrain, maxcorlist); %find segments
