%
% produce a grid of membership values for the matlab contour
% function for fuzzy C-means clusters
%
%   [ grid, x, y ] = fcmalcut( centre, mm, region, nres )
%
% where
%    centre  = cluster centres (from 'fcmeans')
%              (size nclusters x 2)
%    mm      = "fuzziness" measure
%    region  = define 2D region in 'axis' form
%    nres    = contour grid resolution (nres x nres)
%    grid    = grid of membership values for 'contour'
%    x       = X-axis of grid for 'contour'
%    y       = Y-axis of grid for 'contour'
%
% bob newell, june 1997
%
%--------------------------------------------------------------
function [ grid, x, y ] = fcmalcut( centre, mm, region, nres )
%
grid = zeros( nres, nres );
x = [0:nres-1] * ( region(2) - region(1) ) / ( nres - 1 ) + region(1);
y = [0:nres-1] * ( region(4) - region(3) ) / ( nres - 1 ) + region(3);
[ nc, m ] = size( centre );
if m == 2,
%
  for i = 1:nres,
    for j = 1:nres,
      for k = 1:nc,
        d(k) = norm( [x(j) y(i)] - centre(k,:) );
      end;
      for kk = 1:nc,
        s = 0;
        for k = 1:nc,
          s = s + ( d(kk) / d(k) ) ^ ( ( 2 / mm ) - 1 );
        end;
        u(kk) = 1 / s; 
      end;
      grid(i,j) = max( u );
%      [ i j u grid(i,j) ]
    end;
  end;
%
else
  disp( 'Works only for 2D data' );
end;
%

