question on months&days in 2012
Show older comments
I'm unable to complete this assigment; solutions welcome! I've only started using MATLAB last week (so yes, I'm aware this is basic). https://gyazo.com/c707328e01a308366fae5147aef147bc
5 Comments
Danny Browne
on 25 Oct 2018
Good start. Just know that you can use several conditions in one go. For example:
if m<1 | m>12
where | means OR. I think multiple conditions are needed to solve the last part. One approach would be to check if the day is equal to (==) 30 and (&) month equal to (==) one of the months with 30 days in it. You can use "ismember" to check if one number is present in a set of numbers.
Danny Browne
on 25 Oct 2018
One of your conditions basically reads like this:
if A==1 | 2 | 3
That is not how to write a condition.
A loop is executed when the condition returns true. The condition above will always return true, for any month or day. Basically you can test the condition like this:
A=1;
logical(A==0 | 2)
ans =
logical
1
What?! A is not equal 0 nor 2, why does it return true (1)? As the condition is written, true will be returned if any one of the two separate conditions are satisfied, and the logical of anything other than zero (such as 2) returns true.
logical(-1)
ans =
logical
1
The correct way would be
if A==1 | A==2 | A==3
or
if ismember(A,[1 2 3])
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!