function S = shiftmat(m,n)

% SHIFTMAT   Matrix of shifting indices. Useful for moving averages.
%
%  Example:  shiftmat(3,6)   produces the following 3x6 matrix:
%    1  2  3  4  5  6
%    2  3  4  5  6  7
%    3  4  5  6  7  8
%
%  The input arguments specify the matrix size (see ONES, ZEROS).
%  SHIFTMAT(N) is an N-by-N matrix of shifting indices.
%  SHIFTMAT(M,N) or SHIFTMAT([M,N]) is an M-by-N matrix.
%
%  Example: Moving average of width 3
%    data = rand(8,1) ;
%    S = shiftmat(3,8-3+1) ;
%    ma = mean(data(S)) ;
%
%  See also MOVAVG, LOCALREG, ACF, RIDGEMAT.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2000/01/04 14:20 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

%-- Decipher input arguments
if (nargin==1)
  if (length(m)==1)
    n = m ;              % shiftmat(N) --> shiftmat(N,N)
  elseif (length(m)==2)
    n = m(2) ;           % shiftmat([M,N]) --> shiftmat(M,N)
    m = m(1) ;
  else
    error('The first argument must be one- or two-element vector.') ;
  end
elseif (nargin~=2)
  error('At least one input argument expected.') ;
end

%-- Construct the shift matrix
%     1 2 3 4     1 1 1 1     0 1 2 3
%     2 3 4 5  =  2 2 2 2  +  0 1 2 3
%     3 4 5 6     3 3 3 3     0 1 2 3

V = repmat([1:m]',1,n) ;
H = repmat([0:n-1],m,1) ;
S = V + H ;

%%%%% End of SHIFTMAT.M
