Finding the value of a variable for specific dates
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
Stephen23
2015-11-6
编辑:Stephen23
2015-11-6
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
0 个评论
Peter Perkins
2015-11-12
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!