selecting the closest possible date
3 次查看(过去 30 天)
显示 更早的评论
Hi ,
I have the following matrix:
1226 727198
1226 727563
1226 727929
1226 728294
1226 728659
1226 729024
1226 729390
1238 727198
1238 727563
1238 727929
1238 728294
1238 728659
1238 729024
1238 729390
1238 729755
1238 730120
1238 730485
1238 730851
1238 731216
1255 727198
1255 727563
1255 727929
1255 728294
1255 728659
1255 729024
where the first column is an ID and the second is date. I have a date X=728628.Is there a simple line or function that would enable me to select from each unique ID the date which is directly higher than X .i.e i want for each unique ID the date which is higher but closest to 728628. I can do that by writing many lines but i have the feeling that there is a very simple and direct way to do so.
3 个评论
回答(1 个)
Teja Muppirala
2011-4-30
If your matrix is M, and you have the Statistics Toolbox, then:
X = 728628;
grpstats(M(:,2),M(:,1),@(z)min(z(z > X)))
This returns
ans =
728659
728659
728659
Which is the first number higher than 728628 for each of the 3 groups.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!