## Basic Sequential Algorithmic Scheme for clustering using a mean vector
## prototype.

function [clist, ncluster, order] = bsas_proto (x, threshold, maxc, pfunc, cfunc)

   [N,l] = size(x); ## samples are row vectors, cols is number of samples

## randomize the inputs

   newx = x(randomize (1, N),:);
   
## Build a proximity matrix.

##   pmatrix = proxmat (newx, pfunc);
   pmatrix = proxmat (newx);

## Build a membership list for the clusters;

   mlist = zeros (1, N);
   clist = zeros (N, N);
   cmean = zeros (N, l);

 ## initialize - the first cluster is the first x vector
  
   ncluster = 1;
   mlist (1) = 1;
   clist (1,1) = 1;
   csize = zeros (1,N);
   csize (1) = 1;
   cmean (1,:) = x(1,:);

   for ct = 2:N
      minp = 2^20;
      for c = 1:ncluster
         dist = euclidian (cmean (c, :), x(ct, :));
         if (dist < minp)
            minp = dist;
            minc = c;
         endif
      endfor ## for c
               
      if ((minp > threshold) && (ncluster < maxc))
         ncluster = ncluster + 1;
         clist (ncluster,1) = ct; 
         csize (ncluster) = 1;
         cmean (ncluster, :) = x(ct, :);
      else
         cmean (minc, :) = (cmean (minc, :) .* csize (minc) + x(ct,:)) ./ (csize (minc) + 1);
         csize (minc) = csize (minc) + 1;
         clist (minc, csize (minc)) = ct;
      endif   
         
   endfor

endfunction
         


