% COPY makes a (deep) copy of the input matrix objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: COPY makes a deep copy of the input matrix objects.
%
% CALL:        b = copy(a, flag)
%
% INPUTS:      a    - input matrix object
%              flag - 1: make a deep copy, 0: return copies of handles
%
% OUTPUTS:     b - copy of inputs
%
% This is a transparent function and adds no history.
%
% VERSION:     $Id: copy.m,v 1.8 2010/06/25 15:11:47 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = copy(old, deepcopy)
  
  if deepcopy
    % Loop over input matrix objects
    new = matrix.newarray(size(old));
    obj = copy@ltpda_uoh(new, old, 1);
    
    for kk=1:numel(old)
      if ~(isempty(old(kk).objs) && isnumeric(old(kk).objs))
        obj(kk).objs = copy(old(kk).objs, 1);
      end
    end
  else
    obj = old;
  end
  
  varargout{1} = obj;
end

