function [c,ia] = setdiff(a,b,flag)
%SETDIFF Set difference for Java objects.

%   Copyright 1984-2004 The MathWorks, Inc. 
%   $Revision: 1.2.4.1 $  $Date: 2004/01/24 09:22:26 $

nIn = nargin;

if nIn < 2
  error('MATLAB:setdiff:TooFewInputs', 'Not enough input arguments.');
elseif nIn > 3
  error('MATLAB:setdiff:TooManyInputs', 'Too many input arguments.');
end

if nIn == 2
  flag = [];
end

isrows = strcmpi(flag,'rows');

rowsA = size(a,1);
colsA = size(a,2);
rowsB = size(b,1);
colsB = size(b,2);

rowvec = ~((rowsA > 1 && colsB <= 1) || (rowsB > 1 && colsA <= 1) || isrows);

nOut = nargout;

if isempty(flag)
  
  numelA = length(a);
  numelB = length(b);
  
  if numel(a)~=numelA || numel(b)~=numelB
    error('MATLAB:setdiff:AorBinvalidSize',...
        'A and B must be vectors or ''rows'' must be specified.');
  end

  % Handle empty arrays.
  
  if (numelA == 0)
    % Predefine outputs to be of the correct type.
    c = a([]);
    ia = [];
    % Ambiguous if no way to determine whether to return a row or column.
    ambiguous = (rowsA==0 && colsA==0) && ...
      ((rowsB==0 && colsB==0) || numelB == 1);
    if ~ambiguous
      c = reshape(c,0,1);
      ia = reshape(ia,0,1);
    end
  elseif (numelB == 0)
    % If B is empty, invoke UNIQUE to remove duplicates from A.
    if nOut <= 1
      c = unique(a);
    else
      [c,ia] = unique(a);
    end
    return
    
    % General handling.
    
  else

    % Make sure a and b contain unique elements.
    if nOut > 1
      [a,ia] = unique(a(:));
    else
      a = unique(a(:));
    end
    
    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));
    
    % Remove all matching entries
    ndx([d;d+1]) = [];

    d = ndx <= length(a);     % Values in a that don't match.
    
    c = a(ndx(d));
    
    if nOut > 1
      ia = ia(ndx(d));
    end
  end
  
  % If row vector, return as row vector.
  if rowvec
    c = c.';
    if nOut > 1
      ia = ia.';
    end
  end
  
else    % 'rows' case
  if ~isrows
    error('MATLAB:setdiff:UnknownFlag', 'Unknown flag.');
  end
  
  % Automatically pad strings with spaces
  if ischar(a) && ischar(b)
    if colsA > colsB
      b = [b repmat(' ',rowsB,colsA-colsB)];
    elseif colsA < colsB 
      a = [a repmat(' ',rowsA,colsB-colsA)];
      colsA = colsB;
    end
  elseif colsA ~= colsB
    error('MATLAB:setdiff:AandBcolnumMismatch',...
        'A and B must have the same number of columns.');
  end
  
  % Handle empty arrays
  if rowsA == 0
    c = zeros(rowsA,colsA);
    ia = [];
  elseif colsA == 0 && rowsA > 0
    c = zeros(1,0);
    ia = rowsA;
    % General handling
  else
    % Remove duplicates from A; get indices only if needed
    if nOut > 1
      [a,ia] = unique(a,flag);
    else
      a = unique(a,flag);
    end
    
    % Create sorted list of unique A and B; want non-matching entries
    [c,ndx] = sortrows([a;b]);
    [rowsC,colsC] = size(c);
    if rowsC > 1 && colsC ~= 0
      % d indicates the location of non-matching entries
      d = c(1:rowsC-1,:) ~= c(2:rowsC,:);
    else
      d = zeros(rowsC-1,0);
    end
    d = any(d,2);
    d(rowsC,1) = 1;   % Final entry always included.
    
    % d = 1 now for any unmatched entry of A or of B.
    n = size(a,1);
    d = d & ndx <= n; % Now find only the ones in A.
    
    c = c(d,:);
    
    if nOut > 1
      ia = ia(ndx(d));
    end
  end
end

% Automatically deblank strings
if ischar(a)
  c = deblank(c);
end
