function [c,ia,ib] = setxor(a,b,flag)
%SETXOR Set exclusive-or.
%   SETXOR(A,B) when A and B are vectors returns the values that are
%   not in the intersection of A and B.  The result will be sorted.
%   A and B can be cell arrays of strings.
%
%   SETXOR(A,B,'rows') when A are B are matrices with the same number
%   of columns returns the rows that are not in the intersection
%   of A and B.
%
%   [C,IA,IB] = SETXOR(...) also returns index vectors IA and IB such
%   that C is a sorted combination of the elements of A(IA) and B(IB)
%   (or A(IA,:) and B(IB,:)).
%
%   See also UNIQUE, UNION, INTERSECT, SETDIFF, ISMEMBER.

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

%   Cell array implementation in @cell/setxor.m

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

rowvec = 0;
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,1)==1);
  
  % Make sure a and b contain unique elements.
  [a,ia] = unique(a(:));
  [b,ib] = unique(b(:));

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

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

  c = e(ndx);
  if isempty(c), c = c([]); end
  n = length(a);
else
  % Make sure a and b contain unique elements.
  [a,ia] = unique(a,flag);
  [b,ib] = 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
  e = [a;b];
  [c,ndx] = sortrows(e);
  [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

  c = e(ndx,:);
  n = size(a,1);
  
  % Automatically deblank strings
  if isstr(a) & isstr(b),
    c = deblank(c);
  end
end

if nargout>1,
  d = ndx <= n; % Values in a that don't match.
  ia = ia(ndx(d));
  d = ndx > n; % Values in b that don't match.
  ib = ib(ndx(d)-n);
end
if rowvec, c = c.'; ia = ia.'; ib = ib.'; end

