Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

2 ビュー (過去 30 日間)
Ajith Thomas
Ajith Thomas 2019 年 6 月 29 日
閉鎖済み: Image Analyst 2021 年 4 月 12 日
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 件のコメント
Rik
Rik 2021 年 2 月 4 日
Did you use the debugger to run your code line by line for this input?
Panagiotis Papias
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
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 件のコメント
anil Reddy
anil Reddy 2020 年 10 月 21 日
if i take (0<month<=12) in place of ((12>=month) && (month>0)) why am i getting mistake
Rik
Rik 2020 年 10 月 21 日
Because that is not how Matlab operations work. The explanation provided by mlint explains it:

per isakson
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
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
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
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
Chandan Kumar 2021 年 3 月 3 日
looks like you didnt verifyng the input is scalar or not

Capulus_love
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
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
Adonis Thirafi Hugo Mafaza 2020 年 10 月 30 日
Why is my code not working?
  1 件のコメント
Rik
Rik 2020 年 10 月 30 日
Because you didn't use the debugging tools to run your code line by line.

QueenX
QueenX 2020 年 10 月 31 日
編集済み: QueenX 2020 年 10 月 31 日
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
  2 件のコメント
Rik
Rik 2020 年 10 月 31 日
It is a good exercise to write a function like this, but why did you post this? What does it add to the previous answers?
QueenX
QueenX 2020 年 11 月 1 日
I simply think that there are a lot of ways to solve this code. Just want to share my view. Is there any problem? btw

Amrut Umrankar
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 件のコメント
Amrut Umrankar
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
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
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

Chandan Kumar
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
Rik 2021 年 3 月 3 日
Shorter code is possible with a different strategy, see e.g. this answer.
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
Shun Yan 2021 年 4 月 5 日
Why would this code not pass the scalar test? %Return false if the input is not scalar
  3 件のコメント
Shun Yan
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
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
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

Community Treasure Hunt

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

Start Hunting!

Translated by