user3052817
user3052817

Reputation: 245

How do I match up dates of two matrices and find subsequent data in Matlab?

I have two matrices that contain different dates (in Matlab time). The data is daily and is sequential in time for both matrices. The first matrix ranges from the year 1948 to 2015. The second matrix ranges from 2004-2012. How do I find the indices (and related dates) of matrix 1 from matrix 2 (i.e., grab only 2004-2012 dates from the 1948-2015 matrix)? I ultimately need to do this to grab data from matrix 1 that corresponds to the time period of matrix 2 (i.e., (1948-2012 shortened to the 2004-2012 time period).

Example data:

Matrix 1: [711493
711494
711495
711496
711497
...]

Matrix 2: [732113
732114
732115
732116
732117
...]   

Matrix 2 data: [2.02728476987546
1.19049470851012
1.74428201430610
1.91262806946072
1.32636277791079
...]

Upvotes: 0

Views: 59

Answers (1)

jodag
jodag

Reputation: 22314

You could grab the indices of matrix 1 that fall within the date range of matrix 2 by first finding the minimum and maximum dates in matrix 2, then generate a logical array from matrix 1 and convert it to indices using the find function.

dmin = min(Matrix2(:));
dmax = max(Matrix2(:));
idx = find(Matrix1 >= dmin & Matrix1 <= dmax);

Upvotes: 1

Related Questions