function [c,ia] = setdiff(a,b,flag)
%SETDIFF Set difference.
%   SETDIFF(A,B) when A and B are vectors returns the values
%   in A that are not in B. The result will be sorted.  A and B
%   can be cell arrays of strings.
%
%   SETDIFF(A,B,'rows') when A are B are matrices with the same
%   number of columns returns the rows from A that are not in B.
%
%   [C,I] = SETDIFF(...) also returns an index vector I such that
%   C = A(I) (or C = A(I,:)).
%
%   See also UNIQUE, UNION, INTERSECT, SETXOR, ISMEMBER.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.19 $  $Date: 2000/06/01 04:39:57 $

%   Cell array implementation in @cell/setdiff.m

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

if nargin==2, flag=''; end

if nargin==2 | isempty(flag)
  if length(a)~=prod(size(a)) | length(b)~=prod(size(b))
    error('A and B must be vectors or ''rows'' must be specified.');
  end
  rowvec = ~((size(a,1)>1 & size(b,2)<=1) | (size(b,1)>1 & size(a,2)<=1));

  % Make sure a and b contain unique elements.
  [a,ia] = unique(a(:));
  b = unique(b(:));

  % Find matching entries
  [c,ndx] = sort([a;b]);

  % d indicates the location of matching entries
  d = find(c(1:end-1)==c(2:end));
  ndx([d;d+1]) = []; % Remove all matching entries

  d = ndx <= length(a); % Values in a that don't match.
  ia = ia(ndx(d));
  c = a(ndx(d));
  if rowvec, c = c.'; ia = ia.'; end
else
  % Make sure a and b contain unique elements.
  [a,ia] = unique(a,flag);
  b = unique(b,flag);

  % Automatically pad strings with spaces
  if isstr(a) & isstr(b),
    if size(a,2) > size(b,2), 
      b = [b repmat(' ',size(b,1),size(a,2)-size(b,2))];
    end
    if size(a,2) < size(b,2), 
      a = [a repmat(' ',size(a,1),size(b,2)-size(a,2))];
    end
  end

  if size(a,2)~=size(b,2), 
    error('A and B must have the same number of columns.');
  end
  
  % Find matching entries
  [c,ndx] = sortrows([a;b]);
  [m,n] = size(c);
  if m > 1 & n ~= 0
    % d indicates the location of matching entries
    d = c(1:end-1,:)==c(2:end,:);
  else
    d = zeros(m-1,n);
  end
  if ~isempty(d), d = find(all(d,2)); end
  ndx([d;d+1]) = []; % Remove all matching entries

  d = ndx <= size(a,1); % Values in a that don't match.
  ia = ia(ndx(d));
  c = a(ndx(d),:);
  
  % Automatically deblank strings
  if isstr(a) & isstr(b),
    c = deblank(c);
  end
end
