function y = poisspdf(x,lambda)
%POISSPDF Poisson probability density function.
%   Y = POISSPDF(X,LAMBDA) returns the Poisson probability density 
%   function with parameter LAMBDA at the values in X.
%
%   The size of Y is the common size of X and LAMBDA. A scalar input   
%   functions as a constant matrix of the same size as the other input.    
%
%   Note that the density function is zero unless X is an integer.

%   References:
%      [1]  M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
%      Functions", Government Printing Office, 1964, 26.1.22.

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

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

[errorcode x lambda] = distchck(2,x,lambda);

if errorcode > 0
    error('Requires non-scalar arguments to match in size.');
end

y = zeros(size(x));
if (length(y) == 0), return; end
y(lambda < 0) = NaN;

k = (x >= 0 & x == round(x) & lambda >= 0);
% using exp(gammaln) instead of gamma can avoid possible overflow.
% the adding of realmin to 0 cases is to get the effect of 0^0 = 1.
if (any(k))
   y(k) = exp(-lambda(k) + x(k) .* log(lambda(k) + realmin*(lambda(k)==0)) ...
              - gammaln(x(k) + 1));
end