function r = poissrnd(lambda,m,n)
%POISSRND Random matrices from Poisson distribution.
%   R = POISSRND(LAMBDA) returns a matrix of random numbers chosen   
%   from the Poisson distribution with parameter LAMBDA.
%
%   The size of R is the size of LAMBDA. Alternatively, 
%   R = POISSRND(LAMBDA,M,N) returns an M by N matrix. 
%
%   POISSRND uses a waiting time method.

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

%   Copyright (c) 1993-96 by The MathWorks, Inc.
%   $Revision: 2.4 $  $Date: 1996/02/13 23:31:18 $

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

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

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

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

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

%Initialize r to zero.
r = zeros(rows, columns);


p = zeros(rows,columns);
done = ones(rows,columns);

while any(any(done)) ~= 0
    p = p - log(rand(rows,columns));
    kc = [1:rows*columns]';
    k = find(p < lambda);
    if any(k)
        r(k) = r(k) + 1;
    end
    kc(k) = [];
    done(kc) = zeros(size(kc));
end
    

% Return NaN if LAMBDA is not positive.

tmp = NaN;
if any(any(lambda <= 0));
    if prod(size(lambda) == 1)
        r = tmp(ones(rows,columns));
    else
        k = find(lambda <= 0);
        r(k) = tmp(ones(size(k)));
    end
end
