function r = ncfrnd(nu1,nu2,delta,m,n)
%NCFRND Random matrices from the noncentral F distribution.
%   R = NCFRND(NU1,NU2,DELTA) returns a matrix of random numbers chosen   
%   from the noncentral F distribution with parameters NU1, NU2 and DELTA.
%
%   The size of R is the common size of NU1, NU2 and DELTA if all are matrices.
%   If either parameter is a scalar, the size of R is the size of the other
%   parameter. Alternatively, R = NCFRND(NU1,NU2,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. 73-74.

%   B.A. Jones 2-7-95
%   Copyright 1993-2000 The MathWorks, Inc. 
%   $Revision: 2.8 $  $Date: 2000/07/28 19:33:03 $

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

if nargin == 3
    [errorcode rows columns] = rndcheck(3,3,nu1,nu2,delta);
end

if nargin == 4
    [errorcode rows columns] = rndcheck(4,3,nu1,nu2,delta,m);
end

if nargin == 5
    [errorcode rows columns] = rndcheck(5,3,nu1,nu2,delta,m,n);
end

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

%Initialize r to NaN, then fill in valid values.
tmp = NaN;
r = tmp(ones(rows, columns));
if (prod(size(nu1))*prod(size(nu2)) == 1), nu1 = nu1*ones(rows,columns); end
k = (nu1>0 & nu2>0);
if (any(k) & (prod(size(k)) > 1))
   if (prod(size(nu1)) ~= 1),   nu1 = nu1(k); end
   if (prod(size(nu2)) ~= 1),   nu2 = nu2(k); end
   if (prod(size(delta)) ~= 1), delta = delta(k); end
end

r(k) = (ncx2rnd(nu1,delta)./nu1)./ (chi2rnd(nu2) ./ nu2);
