function out = ndhorzr(a,b)
% horzcat for
%  [2-D  N-D]
%  [N-D  2-D]
%  [N-D  N-D]

% Copyright 2003-2004 The MathWorks, Inc.

sza = size(a);
szb = size(b);
nda = ndims(a);
ndb = ndims(b);
exa = sza(3:end);
exb = szb(3:end);
if sza(1)==szb(1)
   exb = [exb ones(1,nda-ndb)];
   exa = [exa ones(1,ndb-nda)];
   nd = max([nda ndb]);
   if all(exa==exb | exa==1 | exb==1)
      aeq1 = find(exa==1);
      repa = ones(1,nd-2);
      repa(aeq1) = exb(aeq1);
      beq1 = find(exb==1);
      repb = ones(1,nd-2);
      repb(beq1) = exa(beq1);
      out = cat(2,repmat(a,[1 1 repa]),repmat(b,[1 1 repb])); 
   else
      error('Invalid extra dimensions')
   end
else
   error('Row Matrix dimensions must agree.');
end

