function varargout = eliminateRows(matrixOriginal, rowsToEliminate, zerotol)
% returns first input matrix with all rows of second input eliminated
%
% Syntax: A = eliminateRows(A, B, zerotol)
%
% This function returns the matrix A for which all rows are eliminated that
%   are present in B. For comparison of rows the each element must fulfill
%   abs(a-b) < zerotol. For normal floating point precision, use eps.
%
% [A, idx] = eliminateRows(..) additionally returns the indices to the rows
%   of B that eliminated a row in A. Only the first occurance of B is 
%   chosen.
% 
% See also: eps, isZeroVector, isZero, obtainOrderedSetMatrix

% MDT: MATLAB developement tools
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU Lesser General Public License as published 
%   by the Free Software Foundation, either version 3 of the License, or 
%   (at your option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License 
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.

iRowOriginal = 1;
idxEliminated = [];
while iRowOriginal <= size(matrixOriginal, 1)
    exist = 0;
    for iRowToEliminate = 1:size(rowsToEliminate, 1)
        if isAllZero(matrixOriginal(iRowOriginal, :) - ...
                     rowsToEliminate(iRowToEliminate, :), zerotol)
            exist = 1;
            break;
        end
    end
    if exist == 1
        matrixOriginal(iRowOriginal, :) = [];
        idxEliminated(end+1) = iRowToEliminate;
    else
        iRowOriginal = iRowOriginal + 1;
    end
end

if isempty(matrixOriginal);matrixOriginal=[];end;

varargout{1} = matrixOriginal;
if nargout == 2, varargout{2} = idxEliminated;end;

end
