year == '2000' is not comparing a single entity to another single entity.
'2001' is not a single entity: it is a character array with size 1 x 4. The test '2001' == '2000' is not checking that the entire "string" is identity: it is a vector test that compares ['2' == '2', '0' == '0', '0' == '0', '1' == '0] returning [true, true, true, false] . Then if [true, true, true, false] considers the condition to be false because at least one entry was false.
Likewise year == '2006' gives [true,true,true,false] and year == '2007' gives [true,true,true,false] . You have an | operation between those two vectors, so the result is [true|true, true|true, true|true, false|false] which gives [true, true, true, false] . And so on through year = '2009', continuing to get those [true, true, true, false] arrays. Then you reach the year == '2010' which is going to give you ['2'=='2','0'=='0,'0'=='1','1'=='0'] which is [true,true,false,false] . You or that with the [true,true,true,false] array you have been building to that point, getting [true|true,true|true,true|false,false|false] which is [true,true,true,false] -- even though the '0' of '01' did not equal the '1' of '10', the or with the previous true value gives true, allowing you to proceed. Now on to the year == '2011' test. ['2'=='2', '0'=='0', '0'=='1', '1'=='1'] which gives [true,true,false,true] . Now or that with the [true,true,true,false] you have been building up, and you get [true|true,true|true,true|false,false|true] and that gives [true,true,true,true] -- although the '0' of '01' does not match the '1' of '11', the or from the previous values gives true, and even though in all previous cases the fourth position was false, the true of comparing the final 1's gives true there, and now you have true in all positions. All of the remaining tests are or tests and they cannot give a false value because you already have all true. So you get to the end of the line with a [true,true,true,true] and if is happy with that because none of the entries are false.
If you want to thing of '2001' as a complete entity that should be compared as a whole to '2000' then you have three choices:
- You can switch to using strcmp() instead of == . Or
- You can switch to using ismember(year, {'2006', '2007', '2008' , '2009', '2010', '2011', '2014', '2015'}, 'rows') . Or
- R2017a or later, you can switch to string entities instead of character vectors:
year = "2001"
if year == "2000"
a = 1
elseif year == "2006" | year == "2007" | year == "2008" | year == "2009" | year == "2010" | year == "2011" | year == "2014" | year == "2015"
a = 2
end
I would suggest to you that using ismember() is easier to maintain.
0 Comments
Sign in to comment.