function tf = ismember(a,s,flag)
%ISMEMBER True for set member.
%   ISMEMBER(A,S) when A is a vector returns a vector containing 1 where
%   the elements of A are in the set S and 0 otherwise.  A and S can be 
%   cell arrays of strings.
%
%   ISMEMBER(A,S,'rows') when A and S are matrices with the same
%   number of columns returns a vector containing 1 where the rows of
%   A are also rows of S and 0 otherwise.
%
%   See also UNIQUE, INTERSECT, SETDIFF, SETXOR, UNION.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.16 $  $Date: 1998/04/02 18:01:49 $

error(nargchk(2,3,nargin));

if nargin==2
  s = double(s);
  if (isreal(s))
    s = sort(s(:));
  else
    [junk,idx] = sort(real(s(:)));
    s = s(idx);
  end

  if (any(isnan(a(:))) | any(isnan(s(:))))
    idx  = find(isnan(a(:)) == 0);
    ainfn = ismembc(a(idx),s(find(isnan(s(:))==0)));
    tf(idx) = ainfn;
    tf(isnan(a(:))) = 0;
    tf = tf;
  else
    tf = ismembc(a,s);
  end 

else
  if ~isstr(flag) | ~strcmp(flag,'rows'), error('Unknown flag.'); end
  tf = logical(zeros(size(a,1),1));
  
  % Automatically pad strings with spaces
  if isstr(a) & isstr(s),
    if size(a,2) > size(s,2), 
      s = [s repmat(' ',size(s,1),size(a,2)-size(s,2))];
    end
    if size(a,2) < size(s,2), 
      a = [a repmat(' ',size(a,1),size(s,2)-size(a,2))];
    end
  end
  if size(a,2)~=size(s,2) % & ~isempty(a) & ~isempty(s)
    error('A and S must have the same number of columns.');
  end

  [m,n] = size(s);
  for i=size(a,1):-1:1
    if ~isempty(s) & n ~= 0
      tf(i) = any(all(s == a(i*ones(m,1),:),2));
    else
      tf(i) = size(s,1)>0;
    end
  end
end





