Weekday determination
이전 댓글 표시
I wnat determination the weekday whne I enter year ,month,and day. how should I do ? I try it
-----------------------------------------------------
year=input('enter year')
month=input('enter month')
day=input('enter day')
switch year
case (1700:1799)
y=4
case (1800:1899)
y=2
case (1900:1999)
y=0
case (2000:2099)
y=6
case (2100:2299)
y=2
case (2300:2399)
y=0
case (2400:2499)
y=6
case (2500:2599)
y=4
case (2600:2699)
y=2
end
x=mod(year,100)
x1=floor(x,4)
switch month
case 1
m=0
case 2
m=3
case 3
m=3
case 4
m=6
case 5
m=1
case 6
m=4
case 7
m=6
case 8
m=2
case 9
m=5
case 10
m=0
case 11
m=3
case 12
m=5
end
d=mod((y+x1+m+day),7)
switch d
case 0
s=Sunday
case 1
s=Monday
case 2
s=Tuesday
case 3
s=Wednesday
case 4
s=Thursday
case 5
s=Friday
case 6
s=Saturday
end
fprintf('%g',s)
----------------------------------------------------------
답변 (4개)
the cyclist
2011년 12월 9일
2 개 추천
Did you think of trying MATLAB's own the weekday() function?
댓글 수: 1
Jan
2011년 12월 9일
Beside the fact that using toolbox function is efficient and stable, the algorithm in WEEKDAY is smart:
d = mod(fix(datenum(year, month, day)) - 2, 7) + 1
Fangjun Jiang
2011년 12월 9일
0 개 추천
datestr(datenum(2011,12,8),'ddd')
Andrei Bobrov
2011년 12월 9일
function [dd,ddd] = dayweekwiki(y,m,d)
dd = rem( ceil( d + 497 * (m / 4800 + y / 400) + 16870921 / 2400 ) - 1 , 7) + 1;
if nargout > 1
ds = {'Monday'
'Tuesday'
'Wednesday'
'Thursday'
'Friday'
'Saturday'
'Sunday'};
ddd = ds(dd);
end
댓글 수: 4
Jan
2011년 12월 9일
Do you have a reference for this method?
Andrei Bobrov
2011년 12월 9일
Hi Jan! Only in russian language:
http://ru.wikibooks.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%B2%D1%8B%D1%87%D0%B8%D1%81%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F_%D0%B4%D0%BD%D1%8F_%D0%BD%D0%B5%D0%B4%D0%B5%D0%BB%D0%B8
Jan
2011년 12월 9일
Thanks, Andrei. I think, I know what "ОСТАТОК 7" means. It reminds me to the "Omega 13".
Andrei Bobrov
2011년 12월 9일
this is "mod 7"
Jan
2011년 12월 9일
Another approach:
function dd = mmm(y,m,d)
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
y = y - (m < 3);
dd = mod(y + floor(y/4) - floor(y/100) + floor(y/400) + t(m) + d, 7) + 1;
About your original code:
switch year
case (1700:1799)
This is no valid Matlab syntax. Better use
if 1700 <= year && year <1800
...
elseif year < 1900
...
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!