function r = nctrnd(v,delta,m,n)
%NCTRND Random matrices from noncentral T distribution.
%   R = NCTRND(V,DELTA) returns a matrix of random numbers chosen   
%   from the noncentral T distribution with parameters V and DELTA.
%
%   The size of R is the common size of V and DELTA if both are matrices.
%   If either parameter is a scalar, the size of R is the size of the other
%   parameter. Alternatively, R = NCTRND(V,DELTA,M,N) returns an M by N  
%   matrix.

%   Reference:
%      [1]  Evans, Merran, Hastings, Nicholas and Peacock, Brian,
%      "Statistical Distributions, Second Edition", Wiley
%      1993 p. 147-148.

%   Copyright 1993-2000 The MathWorks, Inc. 
%   $Revision: 2.7 $  $Date: 2000/05/26 18:53:16 $

if nargin < 2, 
    error('Requires at least two input arguments.');
end

if nargin == 2
    [errorcode rows columns] = rndcheck(2,2,v,delta);
end

if nargin == 3
    [errorcode rows columns] = rndcheck(3,2,v,delta,m);
end

if nargin == 4
    [errorcode rows columns] = rndcheck(4,2,v,delta,m,n);
end

if errorcode > 0
    error('Size information is inconsistent.');
end

%Initialize r to zero.
r = zeros(rows, columns);
if (prod(size(v)) == 1)
   v = v(ones(rows,columns));
end
if (prod(size(delta)) == 1)
   delta = delta(ones(rows,columns));
end

t = (v>0);
if any(t(:))
   r(t) = normrnd(delta(t),1) ./ sqrt(chi2rnd(v(t)) ./ v(t));
end

% Return NaN for values of V that are not positive.
if any(~t(:))
   r(~t) = NaN;
end
