function [chi, MIN]=characteristic(varargin)
% [ chi, MIN ] = characteristic(D, [options])
% Returns the characteristic function of an index set.
%
% Input:
%   D               column vectors defining the coordinates
%
% Options:
%   'amin',val      Index of first entry in D
%   'amax',val      Index of last entry in D
% 
% Info: This function is much faster then the inverse function supp(). Use this function if possible.
%
% E.g.: a=characteristic([1 0; 2 0; 1 4]')
%       [a,amin]=characteristic([1 0; 2 0; 1 -1]','amin',[-1; -1])
%       [a,amin]=characteristic([1 0; 2 0; 1 -1]','amin',[0; 0])
%
% See also: supp
%
% Written by: tommsch, 2016

amin=parsem('amin',varargin,[]);
amax=parsem('amax',varargin,[]);

D=varargin{1};

MIN=min(D,[],2); 
if(~isempty(amin)); 
    if(any(amin>MIN))
        error('''amin'' too big.'); 
    end
    MIN=amin; 
end;
MAX=max(D,[],2); 
if(~isempty(amax)); 
    if(any(amax<MAX))
        error('''amax'' too small.');
    end
    MAX=amax; 
end;


if(~isequal(round(D),D)); chi=[]; return; end;
dim=size(D,1);
if(dim>1); chi=zeros(1+(MAX-MIN)');
else; chi=zeros(1+(MAX-MIN)',1);
end;

D=setplus(D,-MIN)+1;
Dcell=num2cell(D,1);

for i=1:size(Dcell,2);
    Dcelli=num2cell(Dcell{i});
    chi(Dcelli{:})=1;
end

end
function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing. 