Finding the value of a variable for specific dates

4 views (last 30 days)
I have a 2 column matrix (Matlab serial time, evapotranspiration(ETo)), each with the same number if rows. This matrix has over 4,000 rows, and I have about 200 specific dates that I need the ETo value for. I would like to filter out the ETo values with these specific dates, and then create a new 2 column matrix with these dates and ETo values.
I cannot find a command for this, any help would be greatly appreciated!

Answers (2)

Stephen23
Stephen23 on 6 Nov 2015
Edited: Stephen23 on 6 Nov 2015
You could use bsxfun to create a logical index and then use this index to select particular rows of the matrix:
M(X,:) % rows matching those dates
M(~X,:) % rows with different dates
Here is a simple example using two small vectors:
>> M = [99.6,100.2,100.7,102.1,104.9,105.2]; % dates in matrix
>> N = [100,105]; % dates to match
>> tol = 0.5;
>> X = any(abs(bsxfun(@minus,M,N(:)))<=tol,1)
X =
1 1 0 0 1 1
>> M(X)
ans =
99.600 100.200 104.900 105.200
>> M(~X)
ans =
100.70 102.10

Peter Perkins
Peter Perkins on 12 Nov 2015
Stephen's answer went above and beyond, and accounts for a tolerance on the dates. If that is not important, just use ismember to find the rows for the 200 dates you care about. Something like this:
>> X = [1 100; 2 200; 3 300; 4 400; 5 500]
X =
1 100
2 200
3 300
4 400
5 500
>> X(ismember(X(:,1),[2 4]),:)
ans =
2 200
4 400

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!