%  catMtx = ncat(mtx, n)

%

%    Concatenate matrix to itself n times over first dimension

%

%    Input:   mtx    =  matrix

%             n      =  number of times to concatenate

%    Output:  catMtx = concatenated matrix



function catMtx = ncat(mtx, n)



if ((n <= 0) | (n ~= fix(n)) | (ndims(n) ~= 2) | (size(n) ~= [1, 1]))

   error ('ncat: n must be positive integer');

end



catMtx = mtx;

i = 1;

n = n - 1;

while (i <= n)

   catMtx = [catMtx; catMtx];

   n = n - i;

   i = i + i;

end

if (n > 0)

   catMtx = [catMtx; catMtx(1 : (n * size(mtx, 1)), :)];

end

