Write a function called day_diff that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the dif
이전 댓글 표시
I can't deal it anymore. I have trouble to check an integer number or an array number in order to input argument in this problem .
function dd = day_diff(month1, day1, month2, day2)
if (month1 && day1 >0)&&(month1
if true
% code
end <= 12)&&(month2 && day2 >0) && month2 <= 12
if isinteger(month1 &&
if (month1 == 1 && month2 == 1)||(month1 == 3 && month2 == 3)||(month1 == 5 && month2 == 5)||(month1 == 7 && month2 == 7)||(month1 == 8 && month2 == 8)||(month1 == 10 && month2 == 10)||(month1 == 12 && month2 == 12) && day1<=31 && day2<=31
if day1 == day2
total1 = day1;
total2 = day2;
elseif day1 ~= day2
total1 = max(day1,day2);
total2 = min(day1,day2);
end
elseif (month1 == 4 && month2 == 4) ||(month1 == 6 && month2 == 6)||(month1 == 9 && month2 == 9)||(month1 == 11 && month2 == 11) % months have 30 days
if day1 == day2 && day1<=30 && day2<30
total1 = day1;
total2 = day2;
elseif day1 ~= day2 && day1<=30 && day2<30
total1 = max(day1,day2);
total2 = min(day1,day2);
else
dd=-1; return
end
elseif month1 == 1 && month2 ==2
total1 = day1;
total2 = day2+31;
elseif (month1 == 2 && day1<=28) && month2 == 1
total1 = day1 + 31;
total2 = day2;
elseif (month1 == 2 && day1>28) && month2 == 1
dd=-1;
return
elseif month1 == 1 && month2 == 12
total1 = day1;
total2 = day2 + 334;
elseif month1 == 2 && month2 == 3
total1 = day1 + 31;
total2 = day2 + 59;
elseif month1 == 1 && day1<=31&& month2 == 4
total1 = day1;
total2 = day2 + 90;
elseif month1 == 1 && day1>31 && month2 == 4
dd=-1; return
elseif month1 == 11 && day1<=30 && month2 == 12
total1 = day1 ;
total2 = day2 + 30;
elseif month1 == 11 && day1>30 && month2 == 12
dd=-1;
return
elseif month1 == 7 && month2 == 9
total1 = day1 + 181;
total2 = day2 + 243;
elseif month1 == 2 && day1<=28 && month2 == 6 && day2>30
dd=-1; return
elseif month1 == 2 && day1<=28 && month2 == 6 && day2<=30
total1 = day1;
total2 = day2 + 92;
end
dd = (max(total1,total2)) - (min(total1,total2));
else
dd = -1;
return
end
end
답변 (4개)
Image Analyst
2016년 11월 26일
0 개 추천
Why not make your function just be a wrapper for etime()? Did they specifically prohibit using that function?
댓글 수: 4
ledinh lam
2016년 11월 27일
champions2015
2017년 8월 26일
whats the 'floor' for? what difference does it make with using 'fix'?
Image Analyst
2017년 8월 26일
Floor rounds down to minus infinity. Fix rounds towards zero. They give different results when operating on negative numbers.
champions2015
2017년 8월 26일
I see, thanks very much!
Here there is more basic solution:
function dd = day_diff(m1, d1, m2, d2)
A = [31 28 31 30 31 30 31 31 30 31 30 31]';
day1 = d1 + sum(A(1:(m1-1)));
day2 = d2 + sum(A(1:(m2-1)));
if prod(size(m1)) ~= 1 || prod(size(m2)) ~= 1 || prod(size(d1)) ~= 1 || prod(size(d2)) ~= 1
dd = -1;
elseif m1 < 1 || m2 < 1 || d1 < 1 || d2 < 1 || m1 ~= floor(m1) || m2 ~= floor(m2) || d1 ~= floor(d1) || d2 ~= floor(d2)
dd = -1;
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
else
dd = abs(day2-day1);
end
end
댓글 수: 4
Raunil Raj
2018년 3월 6일
%to avoid matrix input for day and month
if prod(size(m1)) ~= 1 || prod(size(m2)) ~= 1 || prod(size(d1)) ~= 1 || prod(size(d2)) ~= 1
dd = -1;
%To avoid negative and rational numbers as input for month & day
elseif m1 < 1 || m2 < 1 || d1 < 1 || d2 < 1 || m1 ~= floor(m1) || m2 ~= floor(m2) || d1 ~= floor(d1) || d2 ~= floor(d2)
dd = -1;
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
What case would the last one be? I do not understand the logic. Please help!
Steven Lord
2018년 3월 6일
If you want to check if something is a scalar, don't compute prod(size(...)). Use the isscalar function instead.
Raunil Raj
2018년 3월 7일
Thank you for you response but I actually wanted to know the logic behind this:
elseif A(m1) < d1 || A(m2) < d2
dd = -1;
Image Analyst
2018년 3월 8일
It says to set dd equal to minus one if A is less than d1 at the m1 index, or if A is less than d2 at the m2 index.
Vignesh M
2018년 5월 4일
if true
function [ dd ] = day_diff( m1,d1,m2,d2 )
% Short circuiting!
N = [31,28,31,30,31,30,31,31,30,31,30,31];
if isscalar(m1) && isscalar(d1) && isscalar(m2) && isscalar(d2) && ... m1 == fix(m1) && d1 == fix(d1) && m2 == fix(m2) && d2 == fix(d2) &&... m1 > 0 && m1 <= 12 && m2 > 0 && m2 <= 12 && ... d1 > 0 && d1 <= N(m1) && d2 > 0 && d2 <= N(m2)
day1 = d1 + sum(N(1:1:m1-1));
day2 = d2 + sum(N(1:1:m2-1));
dd = abs(day1-day2)
else
dd = -1
end
end
RAMAKANT SHAKYA
2019년 2월 8일
function dd=day_diff(m1,d1,m2,d2)
num_of_days=[31,28,31,30,31,30,31,31,30,31,30,31];
dd=-1;
mmm1=round(m1);mmm2=round(m2);ddd1=round(d1);ddd2=round(d2);
if(isscalar(d1)&&isscalar(d2)&&isscalar(m1)&&isscalar(m2)) % checking for valid input
if(d1-ddd1 || d2-ddd2 || m1-mmm1 || m2-mmm2) % checking for valid input
dd=-1;
else
if (m1<1||m1>12||m2<1||m2>12) %checking months
dd=-1;
else
if(d1>num_of_days(m1) || d2>num_of_days(m2) || d1<1 || d2<1) %validating days of respective month
dd=-1;
else
dd1=sum(num_of_days(1:m1));
dd1=dd1+d1-num_of_days(m1);
dd2=sum(num_of_days(1:m2));
dd2=dd2+d2-num_of_days(m2);
if(dd1>=dd2)
dd=dd1-dd2;
else
dd=dd2-dd1;
end
end
end
end
end
카테고리
도움말 센터 및 File Exchange에서 Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!