function [out]=finddiffel(inmat,frequencystatus,flipstatus)

%   finddiffel

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      
if nargin<3;flipstatus = 0;end
if nargin < 2; frequencystatus = 0;end
elements=[];
frequency=[];
a=1;
copy=inmat;
for i=1:numel(inmat)
    if ~isempty(find(inmat(i)==copy))
        frequency(a)=sum(sum((inmat(i)==copy)));
        elements(a)=inmat(i);
        copy(find(inmat(i)==inmat))=9999999999999;
        a=a+1;
    end
end


elements=elements';
frequency=frequency';

if frequencystatus == 1
    [frequencynew,index]=sort(frequency);
    elementsnew=elements(index);
    all=[elements frequency elementsnew frequencynew];
    out=int16(frequencynew);
else
    if ~flipstatus
      out=flipud(sort(elements));
    else
      out=sort(elements);
    end
    if size(out,2)>size(out,1)
        out=out';
    end
end
return
    