mHelpMe
mHelpMe

Reputation: 6668

searching a matrix based on two column values

I have a date number called dt_search and a vector of date numbers called dt_sales.

dt_search is the first of a month say 1st July 2014 (this would be in date number format in Matlab).

dt_sales contains monthly date and day of the month is the last weekday of the month approx. I would like to search dt_sales to see if dt_search can be found in but ignoring the day part of the date.

I was thinking (please tell me if there is a better way) of doing something like the following,

 dt_search = datevec(dt_search) % to get the year and month part [2014 7 1 0 0 0]
 dt_sales = datevec(dt_sales)

So dt_sales would look something like below & I could search the data below for where the year is 2014 and the month is 7 and it would return the index position, which in this case would be 7

 [2014 1 25 0 0 0]
 [2014 2 26 0 0 0]
 [2014 3 28 0 0 0]
 [2014 4 30 0 0 0]
 [2014 5 29 0 0 0]
 [2014 6 23 0 0 0]
 [2014 7 31 0 0 0]
 [2014 8 30 0 0 0]

Upvotes: 0

Views: 29

Answers (1)

Dan
Dan

Reputation: 45762

Use an &:

dt_search = datevec(dt_search);
dt_sales = datevec(dt_sales);

ind = dt_sales(:,1)==dt_search(:,1) & dt_sales(:,2)==dt_search(:,2) 

or

ind = all(dt_sales(:,1:2)==dt_search(:,1:2),2)

Upvotes: 1

Related Questions