Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false. The name of the output argument is valid. If any of the inputs is not
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
unction valid=valid_date(year,month,date)
if nargin==3
valid1=true;
else valid=false;
return
end
v1=[year]; v2=[month]; v3=[date];
if isscalar(v1)==true && isscalar(v2)==true && isscalar(v3)==true
valid2=true;
else valid=false;
return
end
if year>0 && 0<month && month<=12 && 0<date && date<=31
valid3=true;
else valid=false;
return
end
a=year/4; b=year/100; c=year/400;
if rem(year,4)==0 && rem(year,100)~=0
valid4=true;
else valid4=false;
end
if rem(year,100)==0 && rem(year,400)~=0
valid5=true;
else valid5=false;
end
if rem(year,400)==0
valid4=true;
else valid4=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=29) || (month==4||6||9||11 && date<=30) && valid4==true && valid5==false
valid6=true;
else valid6=false;
end
if (month==1||3||5||7||8||10||12 && date<=31) || (month==2 && date<=28) || (month==4||6||9||11 && date<=30) && valid5==true && valid4==false
valid7=true;
else
valid7=false;
end
if valid1==true && valid2==true && valid3==true && valid6==true
valid=true;
elseif valid1==true &&valid2==true && valid3==true && valid7==true
valid=true;
else
valid=false;
return
end
why this code is not working for 2018/4/31 and 2003/2/29? and other random dates. but is works for non scalar and random leap years
8 件のコメント
Most of the bugs in your code have been thoroughly discussed before:
What is the point in renaming the data?:
v1=[year]; v2=[month]; v3=[date];
Julian Veran
2020 年 5 月 18 日
function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d)) isvalid = false;
% Check that inputs are positive elseif ~all([y, m, d] > 0) isvalid = false;
% Check that inputs are integers (not the data type) elseif any(rem([y, m, d], 1)) isvalid = false;
% Check that m and d are below the max possible elseif (m > 12) || (d > 31) isvalid = false;
% The inputs could be a valid date, let's see if they actually are else
% Vector of the number of days for each month daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0)) daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay isvalid = false;
else isvalid = true;
end
end
end
Walter Roberson
2020 年 5 月 18 日
Julian,
before posting, please use the MATLAB editor to "smart indent". Then when you post, first click on the '>' button in the CODE toolbar section of the Answers editor, and then paste the code into the highlight box that appears: that will cause the code to be posted in formatted form.
Amrut Umrankar
2020 年 11 月 15 日
function valid = valid_date(year, month, day)
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day) || year ~=fix(year) || month ~=fix(month) || day ~=fix(day)
valid = false;
return;
end
if month <= 0 || month >= 13 || day <= 0 || year <=0
valid = false;
return;
end
ma =0;
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
ma = 1;
elseif month == 2
ma = 2;
elseif month == 4 || month == 6 || month == 9 || month == 11
ma= 3;
end
if ma == 1
if day >= 32
valid = false;
else
valid = true;
end
end
if ma == 2
if mod( year , 4 )
if day >= 29
valid = false;
else
valid = true;
end
end
if ~mod( year , 4 )
if ~mod(year , 100)
if ~mod(year, 400)
if day >= 30
valid = false;
else
valid = true;
end
else
if day >= 29
valid = false;
else
valid = true;
end
end
else
if day >= 30
valid = false;
else
valid = true;
end
end
end
end
if ma == 3
if day >= 31
valid = false;
else
valid = true;
end
end
Panagiotis Papias
2021 年 2 月 4 日
function [valid]=valid_date(year,month,day)
leap_year = false; % We set the leap year as false
if nargin == 3
% Year prerequisites:
if ~isscalar(year) || year ~= fix(year) || year < 0, valid=false; %error('Invalid input as a year');
else
% leap year prerequisites:
if rem(year,4) == 0
if rem(year,400) == 0, leap_year=true;
else
if rem(year,100) == 0, leap_year=false;
else leap_year= true;
end
end
end
% Month prerequisites:
if ~isscalar(month) || month ~= fix(month) || month < 0 || month > 12, valid = false; %error('Invalid input as a month');
else
% Day prerequisites:
if ~isscalar(day) || day ~= fix(day) || day < 0 || day > 31, valid = false; %error('Invalid input as a day');
else
% months with odd (i.e.31) number of days:
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12 && day <= 31, valid = true;
else,valid = false; end
% months with even (i.e.30)number of days:
if month == 4 || month == 6 || month == 9 || month == 11 && day <= 30; valid = true;
else, valid = false; end
% February -special- Prerequisites:
if month == 2 && day > 28 && leap_year == false, valid = false; %error('February has maximum 28 days');
elseif month == 2 && day > 29 && leap_year == true, valid = false; %error('February, for a leap year has at maximum 29 days');
else,valid = true; end
end
end
end
else
valid = false; %error('The number of input arguement has to be 3 and be with this order: "year","month","day"');
end
end
Panagiotis Papias
2021 年 2 月 4 日
The code is running ok, but when it comes to test:
valid_date(2007, 4, 31) fails to comply :/ i am stuck for hours :(((
Rik
2021 年 2 月 4 日
Did you use the debugger to run your code line by line for this input?
Panagiotis Papias
2021 年 2 月 8 日
Tbh, no you are right i have to get more familiar with the debugging. However, i followed sb's else instructions and i managed to solve the assignment 1 hr later. I think i got it what i did wrong
回答 (11 件)
SANTOSH KAMBLE
2020 年 5 月 3 日
編集済み: SANTOSH KAMBLE
2020 年 5 月 4 日
function [valid]=valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif y>=1 && m==3 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==4 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==5 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==6 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==7 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==8 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==9 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==10 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==11 && d>=1 && d<=30
valid=true;
elseif y>=1 && m==12 && d>=1 && d<=31
valid=true;
else
valid=false;
end
end
15 件のコメント
Walter Roberson
2020 年 5 月 3 日
when y mod 4 is nonzero then y mod 100 cannot be 0, since 100 is 4*25
SANTOSH KAMBLE
2020 年 5 月 4 日
Thanks for bringing it to my notice,now it's been resolved.
Walter Roberson
2020 年 5 月 4 日
I would point out to you that if you were to use
if y < 1 | m < 1 | d < 1 | m > 12 | d > 31
valid = false;
elseif .....
end
then you would not have to keep rechecking y>=1 & d>=1 and that you would be able to skip the d<=31 on months that have 31 days
I would also point out that
if m == 1 || m == 3 || m == 5 (and so on)
handles a lot of cases all in one place without needing to test month by month.
Santosh Kamble
2020 年 5 月 5 日
That's a very useful suggestion. Thanks a lot.
Fakhrul Alam Anik
2020 年 5 月 22 日
You didn't write code for "integer" input. It will show any fraction date as valid.
JIGNESH PATIL
2020 年 5 月 22 日
I am getting incorrect answer only for the last day of every month. What changes needs to be done in the above syntax? Anyone?
Walter Roberson
2020 年 5 月 22 日
JIGNESH PATIL: what code are you using?
Saurov Baidya
2020 年 7 月 23 日
nice ....this was simple of all... but lengthy
Saurov Baidya
2020 年 7 月 23 日
function [valid] = valid_date(y, m, d)
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)
valid = false
return
end
if y>=1 && ismember(m,[1 3 5 7 8 10 12]) && d>=1 && d<=31
valid=true;
elseif y>=1 && ismember(m,[4 6 9 11]) && d>=1 && d<=30
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
else valid = false
return
end
end
Walter Roberson
2020 年 7 月 23 日
Notice that if you were to use
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d) || y < 1
then you would not have to keep checking y>=1
Khaled Bin Easin
2020 年 8 月 18 日
function valid = valid_date(year,month,day)
if isscalar(year) && isscalar(month) && isscalar(day)
valid = year>0 && ((12>=month) && (month>0)) && ((31>=day) && (day>0)) && year==fix(year) && month==fix(month) && day==fix(day) && (((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && day<=31) || ((month==4 || month==6 || month==9 || month==11) && day<=30) || (((((rem(year,4)==0 && rem(year,100)~=0) || (rem(year,400)==0)) && day<=29) || (day<=28)) && month==2));
else
valid=false;
end
What about this code!!
Gourav Soni
2020 年 9 月 25 日
This is good and simple
Shaked Baharav
2020 年 9 月 27 日
Hello, Can you explain to me what you did in the code?
anil Reddy
2020 年 10 月 21 日
if i take (0<month<=12) in place of ((12>=month) && (month>0)) why am i getting mistake
Rik
2020 年 10 月 21 日
Because that is not how Matlab operations work. The explanation provided by mlint explains it:

per isakson
2020 年 7 月 29 日
編集済み: per isakson
2020 年 7 月 29 日
Would this function pass?
function [ valid, dt ] = valid_date( y, m, d )
% The names of the input arguments, (year,month,day), are they
% mandatory? Are these names chosen to shadow functions in the
% finance toolbox?
try
% datetime is "smart". Doc says: "Each element of DateVector
% should be a positive or negative integer value [...]. If an
% element falls outside the conventional range, datetime adjusts
% both that date vector element and the previous element."
dt = datetime( y, m, d );
% If datetime didn't adjust any element the input is a
% valid date.
% valid = all( [ year(dt)==y, month(dt)==m, day(dt)==d ] );
vec = datevec( dt );
valid = all( [ vec(1)==y, vec(2)==m, vec(3)==d ] );
catch
% datetime throws an exception when not all input values are
% integers
dt = datetime.empty;
valid = false;
end
end
1 件のコメント
Rik
2020 年 8 月 13 日
I would hope so. As an instructor I would applaud lateral thinking like this. Just as I would accept code where someone did this:
for n=1%use a loop because it is a requirement
output=sum(data,2);
end
Iccu OUMOUACHA
2020 年 5 月 25 日
function valid=valid_date(year, month, day)
if year<=0 || month<=0 || day<=0 || mod( year , 1 )~=0 || mod( month , 1 )~=0 || mod( day , 1 )~=0
valid=false;
return
end
if month<=12 && (ismember(month, [4 6 9 11]) && ismember(day, [1:30]))
valid=true;
elseif month<=12 && (ismember(month, [1 3 5 7 8 10 12]) && ismember(day, 1:31))
valid=true;
elseif month==2 && (mod(year,4)==0 && mod(year,100)~=0 || mod(year,400)==0 && mod(year,100)==0) && ismember(day, 1:29)
valid=true;
elseif month==2 && ismember(day, 1:28)
valid=true;
else
valid=false;
return
end
end
it works in Matlab, when I test a non-scalar inputs, it return false. But here it doesn't work!!!!! ((Assessment 2))
What is the problem?

4 件のコメント
Rik
2020 年 5 月 25 日
It doesn't return false, it returns an error:
valid_date([2020 2020], 11, 5)
Iccu OUMOUACHA
2020 年 5 月 25 日
the problem remained
Rik
2020 年 5 月 25 日
Then you should change your code.
If you think this comment is unhelpful: show the code you used to ensure the output is false for non-scalar inputs. That is the only way someone will be able to help you.
Chandan Kumar
2021 年 3 月 3 日
looks like you didnt verifyng the input is scalar or not
Capulus_love
2020 年 8 月 11 日
編集済み: per isakson
2020 年 8 月 11 日
function x = valid_date(year,month,day)
if nargin == 3
if isscalar(year) && isscalar(month) && isscalar(day)
if month > 0 && month <= 12 && day >= 0 && year > 0
if (month == 1 || 3 || 5 || 7 || 8 || 10 || 12 && day <= 31)...
|| (month == 2 && day <= 29) && (month == 4 || 6 || 9 || 11 && day <= 30)
c0 = mod(year,4)
c1 = mod(year,100)
c2 = mod(year,400)
if (c0 == 0 && c1 ~=0) || (c1 == 0 && c2 ~= 0) || c2 == 0
x = 'true'
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
else
x = 'false'
end
end
why this code not working? :(
1 件のコメント
Rik
2020 年 8 月 11 日
Let's try some cases that will likely point us to an issue:
valid_date(2020,2,29) %returns true
valid_date(2021,2,29) %returns false
valid_date(2021,2,28) %returns false
That last one is a problem. Can you follow the flow of your code where it should mark Feb 29 as valid only in leap years, and the rest of Feb as valid every year?
Also, your code returns the value as a char array, not a logical.
Adonis Thirafi Hugo Mafaza
2020 年 10 月 30 日
0 投票
Why is my code not working?

1 件のコメント
Rik
2020 年 10 月 30 日
Because you didn't use the debugging tools to run your code line by line.
I did spend whole noon to make it work. Even though it is quite long, hope it is useful for someone.
function valid = valid_date(year, month, day)
if ~isscalar(year) || year ~= fix(year)||year<=0
valid = false;
elseif ~isscalar(month) || month >12 || month<=0 || month ~= fix(month)
valid = false;
elseif ~isscalar(day) || day >31 || day ~= fix(day)|| day<=0
valid = false;
elseif day >29 && month == 2 && rem(year,4) == 0 && rem(year,100)~=0 %leap year
valid = false;
elseif day>29 && month == 2 && rem(year,400) == 0 %leap year
valid =false;
elseif day>30 && (month == 4 ||month == 6||month == 9 ||month == 11)
valid =false;
elseif day>28 && month==2 && rem(year,100) == 0 && rem(year,4)==0 && rem(year,400)~=0 %non leap year
valid =false;
elseif day>28 && month==2 && rem(year,4)~=0 && rem(year,400)~=0 %non leap year
valid =false;
else
valid = true;
end
end
Amrut Umrankar
2020 年 11 月 15 日
編集済み: Amrut Umrankar
2020 年 11 月 15 日
function valid = valid_date(year, month, day)
if ~isscalar(year) || ~isscalar(month) || ~isscalar(day) || year ~=fix(year) || month ~=fix(month) || day ~=fix(day)
valid = false;
return;
end
if month <= 0 || month >= 13 || day <= 0 || year <=0
valid = false;
return;
end
ma =0;
if month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12
ma = 1;
elseif month == 2
ma = 2;
elseif month == 4 || month == 6 || month == 9 || month == 11
ma= 3;
end
if ma == 1
if day >= 32
valid = false;
else
valid = true;
end
end
if ma == 2
if mod( year , 4 )
if day >= 29
valid = false;
else
valid = true;
end
end
if ~mod( year , 4 )
if ~mod(year , 100)
if ~mod(year, 400)
if day >= 30
valid = false;
else
valid = true;
end
else
if day >= 29
valid = false;
else
valid = true;
end
end
else
if day >= 30
valid = false;
else
valid = true;
end
end
end
end
if ma == 3
if day >= 31
valid = false;
else
valid = true;
end
end
3 件のコメント
Rik
2020 年 11 月 15 日
Why did you post this? What does it teach? Why should it not be deleted?
Amrut Umrankar
2020 年 11 月 15 日
It is just Another way to solve same problem, As I have used pretty basic coding that's why it is big. It can be optimized.
Rik
2020 年 11 月 15 日
Big code is not a problem. I think undocumented code is a problem if you want to teach people something. You don't explain what your code is doing. Your choice of variable names also doesn't help: why use ma if you can use something more descriptive like MonthType? I know you are determining if a year is a leap year, but you don't explain that. If someone is inexperienced enough to scroll down all the way to your answer, you can't assume that would be clear to them.
If you are posting a complete solution to a homework question, at least try to teach something, instead of only providing the opportunity for cheating.
ABHIJIT BISWAS
2020 年 11 月 29 日
function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d))
isvalid = false;
% Check that inputs are positive
elseif ~all([y, m, d] > 0)
isvalid = false;
% Check that inputs are integers (not the data type)
elseif any(rem([y, m, d], 1))
isvalid = false;
% Check that m and d are below the max possible
elseif (m > 12) || (d > 31)
isvalid = false;
% The inputs could be a valid date, let's see if they actually are
else
% Vector of the number of days for each month
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb
if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0))
daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay
isvalid = false;
else
isvalid = true;
end
end
end
0 件のコメント
Chandan Kumar
2021 年 3 月 3 日
function valid = valid_date(year,month,date)
y=year;m=month;d=date;
if ~isscalar(y)|| ~isscalar(m) || ~isscalar(d)
valid=false;
return
end
if y>=1 && m==1 && d>=1 && d<=31
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=28 && mod(y,4)~=0
valid=true;
elseif y>=1 && m==2 && d>=1 && d<=29 && ((mod(y,4)==0 && mod(y,100)~=0 )|| mod(y,400)==0)
valid=true;
elseif m<=12 && (ismember(m, [4 6 9 11]) && ismember(d, [1:30]))
valid=true;
elseif m<=12 && (ismember(m, [1 3 5 7 8 10 12]) && ismember(d, 1:31))
valid=true;
else
valid = false
end
% This is the shortest and oring code for all the valid date i could write
% date formaat should be in lie vald_date(year,month,date) to mae the function work
1 件のコメント
Rik
2021 年 3 月 3 日
My opinion would be that the shortest code is not always the best code. The best code in my opinion would be maintainable (so well-commented), as well as fast.
Shun Yan
2021 年 4 月 5 日
0 投票

Why would this code not pass the scalar test? %Return false if the input is not scalar
3 件のコメント
Walter Roberson
2021 年 4 月 5 日
if d==fix(d)&&e~=fix(e)
Suppose d is a vector. Then d==fix(d) would result in a vector the same size. Then you would have the && operation. But the && operation can only be used when both sides evaluate to scalars (exception: if the left side evalutes to scalar false, then with && the right side is ignored.)
Shun Yan
2021 年 4 月 5 日
oh so tthe isscalar can't be used in tthat whole bunch of &&? So can i just write a seperate if statements with all tthe ~isscalars att the front, I think that'll work right?
Walter Roberson
2021 年 4 月 5 日
You need to have the isscalar() check before the other checks.
if ~all(iscalar(a) && isscalar(b) && isscalar(c))
do whatever appropriate for error
end
freddy alexander rodriguez torres
2021 年 4 月 12 日
編集済み: freddy alexander rodriguez torres
2021 年 4 月 12 日
function valid=valid_date(y,m,d)
k=y/4;
j=y/400;
i=y/100;
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d) || y~=fix(y) || m~=fix(m) || d~=fix(d)
valid=false;
elseif (k==fix(k) || j==fix(j)) && m==2 && d<=29 && i~=fix(i) && d>0
valid=true;
elseif j==fix(j) && m==2 && d<=29 && d>0
valid=true;
elseif (ismember(m, [1 3 5 7 8 10 12])) && d<=31 && d>0
valid=true;
elseif (ismember(m, [4 6 9 11])) && d<=30 && d>0
valid=true;
elseif m==2 && d<=28 && i==fix(i) && d>0
valid=true;
elseif m==2 && d<=28 && i~=fix(i) && d>0
valid=true;
else
valid=false;
end
0 件のコメント
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!