function r = chi2rnd(v,m,n);
%CHI2RND Random matrices from chi-square distribution.
%   R = CHI2RND(V) returns a matrix of random numbers chosen   
%   from the chi-square distribution with V degrees of freedom.
%   The size of R is the size of V.
%   Alternatively, R = CHI2RND(V,M,N) returns an M by N matrix. 

%   CHI2RND calls GAMRND since the chi-square distribution
%   is a special case of the gamma distribution. (See Devroye, page 403)

%   References:
%      [1]  L. Devroye, "Non-Uniform Random Variate Generation", 
%      Springer-Verlag, 1986

%   Copyright 1993-2000 The MathWorks, Inc. 
%   $Revision: 2.9 $  $Date: 2000/05/26 17:28:34 $

if nargin < 1,
    error('Requires at least one input argument.'); 
end

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

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

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

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

% Call for gamma random numbers.
r = gamrnd(v ./ 2,2,rows,columns);
