Generating days based on leap years

1 回表示 (過去 30 日間)
Damith
Damith 2014 年 9 月 15 日
コメント済み: Star Strider 2014 年 9 月 19 日
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.

採用された回答

Star Strider
Star Strider 2014 年 9 月 16 日
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];
  12 件のコメント
Damith
Damith 2014 年 9 月 19 日
Thanks again but this is not the result I want. The output matrix needs to have 18263 rows and 200 columns. Please see the attached excel file. Number of rows changes from 18262 to 18263 alternatively for 50 year segments from 1998 to 10000 years.
Star Strider
Star Strider 2014 年 9 月 19 日
Your Excel sheet doesn’t exactly match your description, but this seems to do what you want:
yr1 = 1998;
epok = 50;
Mprev = [0 0];
for k1 = 1:epok
yr = yr1:yr1+49;
cyr = mod(yr,100)==0; % Century Years
clpyr = mod(yr,400)==0; % Century Leap Year
lpyr = (mod(yr,4)==0) + (clpyr - cyr); % Leap Years Vector
dpy = ones(size(yr))*365+lpyr; % Create ‘Days/Year’ + Leap Years Vector
csd = cumsum(dpy); % Sum ‘Days/Year’
M = [[0 csd(1:end-1)]'+1 csd'];
Mepok(k1,:) = sum([Mprev; [M(1,1) M(end,2)]],1);
Mprev = [Mepok(max([1 k1]),2) Mepok(end,2)];
Mout(k1,:) = [k1 Mepok(k1,:) (yr+k1-1):50:(yr+k1-1)+200*(epok-1)];
end
Mout % Display Output Matrix
I tweaked my previous code, and created a new output matrix ‘Mout’.
I would never have understood what you want without the spreadsheet.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by