0

I'm trying to take out two separate years from a date table.

% Date table
Datez = [2001 2;2001 5;2001 9;2001 11;2002 3;2002 5;2002 7;2002 9;2002 11;...
2003 2;2003 4;2003 6;2003 8;2003 10;2003 12;2004 3;2004 5;2004 7;...
2004 9;2004 11; 2005 10;2005 12]

I want to take out all values as 1 or 0. I want the dates from 2001-11 to 2002-11 plus all values from 2004-11 to 2005-11. In total I should get a new vector, called test:

test = [0;0;0;1;1;1;1;1;1;0;0;0;0;0;0;0;0;0;0;1;1;0] % final result

I tried these combinations, but I don't know how to combine these four statements into a vector that looks like "test" or if there are any better solutions?

xjcr = 1:length(Datez)
(Datez(xjcr,1) >= 2001 & Datez(xjcr,2) >= 11) % greater than 2001-11 
(Datez(xjcr,1) <= 2002 & Datez(xjcr,2) <= 11) % smaller than 2002-11 

(Datez(xjcr,1) >= 2004 & Datez(xjcr,2) >= 11) % greater than 2004-11 
(Datez(xjcr,1) <= 2005 & Datez(xjcr,2) <= 11) % smaller than 2005-11 

Any ideas are much appreciated, thanks in advance!

2
  • How does 1 and 0 defined in your test vector? Commented Mar 6, 2015 at 0:49
  • if you for example use command Datez(xjcr,1) >= 2004 it will give you a vector of 1 and 0. You simply name this new vector, so test = Datez(xjcr,1) >= 2004 Commented Mar 6, 2015 at 9:20

1 Answer 1

2

Your issue is that you do not want to filter on two items independently, years greater than 2001 and months greater than November. This would give you December 2001 but not January 2002. The solution I believe is to treat your two composite numbers as a single number so that the comparison operator can operate on them as a pair. Here is an easy method:

Datez2 = Datez(:,1)*100 + Datez(:,2);
test = (Datez2>=200111 & Datez2<=200211) | (Datez2>=200411 & Datez2<=200511)

Maybe multiplying by 12 and adding (month - 1) would be best depending on if you are building something that needs to be very robust or if you are just hacking something together.

Sign up to request clarification or add additional context in comments.

1 Comment

@tallblo Thanks for the idea, much appreciated!!, combine them seem to be the trick. Works very good!! Can't do it better myself. I understand the multiply 12, but I don't get, why adding (month - 1). Thanks again for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.