function B = repmat(A,M,N)
%REPMAT Replicate and tile an array.
%   B = REPMAT(A,M,N) replicates and tiles the matrix A to produce the
%   M-by-N block matrix B.
%   
%   B = REPMAT(A,[M N]) produces the same thing.
%
%   B = REPMAT(A,[M N P ...]) tiles the array A to produce a
%   M-by-N-by-P-by-... block array.  A can be N-D.
%
%   REPMAT(A,M,N) when A is a scalar is commonly used to produce
%   an M-by-N matrix filled with A's value.  This can be much faster
%   than A*ONES(M,N) when M and/or N are large.
%   
%   Example:
%       repmat(magic(2),2,3)
%       repmat(NaN,2,3)
%
%   See also MESHGRID.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.12 $  $Date: 1998/02/25 15:58:54 $

if nargin < 2
   error('Requires at least 2 inputs.')
elseif nargin == 2
  if length(M)==1
     siz = [M M];
  else
     siz = M;
  end
else
  siz = [M N];
end

if length(A)==1
  nelems = prod(siz);
  if nelems>0
    % Since B doesn't exist, the first statement creates a B with
    % the right size and type.  Then use scalar expansion to
    % fill the array.. Finally reshape to the specified size.
    B(nelems) = A; 
    B(:) = A;
    B = reshape(B,siz);
  else
    B = A(ones(siz));
  end
elseif ndims(A)==2 & length(siz)==2
  [m,n] = size(A);
  mind = (1:m)';
  nind = (1:n)';
  mind = mind(:,ones(1,siz(1)));
  nind = nind(:,ones(1,siz(2)));
  B = A(mind,nind);
else
  Asiz = size(A);
  Asiz = [Asiz ones(1,length(siz)-length(Asiz))];
  siz = [siz ones(1,length(Asiz)-length(siz))];
  for i=length(Asiz):-1:1
    ind = (1:Asiz(i))';
    subs{i} = ind(:,ones(1,siz(i)));
  end
  B = A(subs{:});
end
