function x = eomday(y,m)
%EOMDAY End of month.
%   X = EOMDAY(Y,M) returns the last day of the month for the given
%   year, Y, and month, M. 
%
%   See also WEEKDAY, DATENUM, DATEVEC.

%   Author(s): C.F. Garvin, 3-27-95
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.7 $  $Date: 1997/11/21 23:48:05 $

if nargin < 2
  error('Please enter Y and M.')
end
if any(m(:) < 0) | any(m(:) > 12) | any(m(:) ~= floor(m(:)))
  error('M must contain integer values between 0 and 12.');
end
y = fix(y);
m = fix(m);

% Do scalar expansion
if length(y)==1, y = y(ones(size(m))); end
if length(m)==1, m = m(ones(size(y))); end

e = find(m == 0); if ~isempty(e), m(e) = ones(size(e)); end

[a,b] = size(y);
y = y(:);
m = m(:);
[ro,co] = size(y);
c = [y m ones(ro,1)];

% Determine number of days in the month.
dpm = [31 28 31 30 31 30 31 31 30 31 30 31];
x = dpm(c(:,2));
i = find((c(:,2) == 2) & ((rem(c(:,1),4) == 0 & rem(c(:,1),100) ~= 0) | rem(c(:,1),400) == 0));
if ~isempty(i)
  x(i) = 29*ones(size(i));
end
x = reshape(x,a,b);
