MATLAB: Generating days based on leap years

daysgeneratingleap years

Hi,
I wanted to generate a matrix like below. Basically, years in the first column, start days, end days for the year by conditional check of first column. Fourth row is basically MOD function to check if a leap year or not.
1960 1 366 366
1961 367 731 365
1962 732 1096 365
1963 1097 1461 365
1964 1462 1827 366
1965 1828 2192 365
1966 2193 2557 365
. . . .
2014
Does anybody have an idea?
Thanks in advance.

Best Answer

There may be more efficient approaches, but this works:
yr = [1960:2014]';
lpyr = (mod(yr,4)==0);
dpy = ones(size(yr))*365+lpyr;
csd = cumsum(dpy);
M = [yr [1; csd(1:end-1)+1] csd dpy];