question on months&days in 2012

1 Ansicht (letzte 30 Tage)
Danny Browne
Danny Browne am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 2018
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 Kommentare
Danny Browne
Danny Browne am 25 Okt. 2018
https://gyazo.com/5091c0e2307f768e0001307c17cb60d3 Think I got it; thanks for the tips!
jonas
jonas am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 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])

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

jonas
jonas am 25 Okt. 2018
Bearbeitet: jonas am 25 Okt. 2018
Now that you've basically solved it, here is my (hopefully correct) solution. Note that for scalars you can write && instead of &. It is a bit faster, because not all conditions are necessarily checked.
d = 31;
m = 4;
% Check day
if d>=0 && d<=31
disp('OK')
else
disp('not OK')
end
% Check month
if m>=1 && m<=12
disp('OK')
else
disp('not OK')
end
% Check day & month
if d==31 && ~ismember(m,[1 3 5 7 8 10 12])
disp('not OK')
elseif (d==30 || d==29) && m==2
disp('not OK')
else
disp('OK')
end

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by