To check Scalar or Not
Show older comments
I have debugged the code and now the Scalar test for this code is failing although I have given a provition in the 3rd line of the code to check wheather the given inputs are scalar or not.
please help!
function valid = valid_date(year,month,day)
if nargin==3
if fix(month) && isscalar(month) && fix(day) && isscalar(day) && fix(year) && isscalar(year) && isscalar(valid_date) && year>0 && month>0 && day>0
if mod(year,4)==0&&mod(year,100)~=0 || mod(year,400)==0&&mod(year,100)==0
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif month==2 && ismember(day,[1:29])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
else
valid=false;
end
else
if ismember(month,[1,3,5,7,8,10,12]) && ismember(day,[1:31])
valid=true;
elseif ismember(month,[4,6,9,11]) && ismember(day,[1:30])
valid=true;
elseif month==2 && ismember(day,[1:28])
valid=true;
else
valid=false;
end
end
else
valid=false;
end
else
valid=false;
end
Assessment result: correctVarious inputs
Assessment result: incorrectNon-scalar
Return false if an input is not scalar...
Assessment result: correctThe last day of every month
Assessment result: correctRandom leap years
Assessment result: correctRandom non-leap years
Assessment result: correctRandom dates
6 Comments
What is this supposed to test for?:
isscalar(valid_date)
You recursively call the function (with no input arguments) and check if the function output is scalar. What is that supposed to detect?
Note that these square brackets are superfluous:
[1:31]
^ ^ not needed!
Rakeshwar Elango
on 29 Jun 2019
Akuroma George
on 20 Mar 2020
Hello Rakeshwar, were you able to finally figure this assignment out? If yes, what worked for you.
I am also stuck here and i don't know what else t0 do.
Nisarg Dalal
on 28 Apr 2020
Hello Rakeshwar. I too am stuck at this same error.
If you were able to figure out the problem, let me know what you did
Sunney Kumar
on 12 Jun 2020
Edited: Sunney Kumar
on 12 Jun 2020
funtion valid = valid_date(year, month, day)
[i, j] = size(year);
[k, l] = size(month);
[o, p] = size(day);
if (i == 1 && j ~= 1) || (k == 1 && l ~= 1) || (o == 1 && p ~= 1)
valid = false
else
%rest of the code
end
Try this piece of code to find out about whether input is scalar or not.
Hakan Karapinar
on 17 Jun 2020
if (~isscalar(year) || year < 1 || year ~= fix(year) ) || (~isscalar(month) || month < 1 || month ~= fix(month) ) || (~isscalar(day) || day < 1 || day ~= fix(day) )
valid = false
return ;
check my code out bro.we take same course.:D its worked i passed week 6
Accepted Answer
More Answers (1)
iitm_pnkj
on 18 Jul 2021
function valid = valid_date(year, month, day)
m31 = [1 3 5 7 8 10 12];
m30 = [4 6 9 11];
if isscalar(year) == 1 && isscalar(month) == 1 && isscalar(day) == 1 && year > 0 && month > 0 && day > 0
if (mod(year,100) == 0 || mod(year,4) ~= 0) && month == 2 && day <=28
valid = true;
elseif ((mod(year,100) ~= 0 && mod(year,4) == 0) || (mod(year,400) == 0)) && month == 2 && day <= 29
valid = true;
elseif day <= 31 && any(m31(:)== month) == 1
valid = true;
elseif day <= 30 && any(m30(:)== month) == 1
valid = true;
else
valid = false;
end
else
valid = false;
end
end
1 Comment
Walter Roberson
on 18 Jul 2021
All those == 1 are unneeded. isscalar() already returns false or true.
The comparisons to 0 are valid, though.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!