How to sort by time
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I have below data,I want to select two data points above and below to the specified time.
2015/8/5 12:13 2
2015/6/13 22:13 5
2015/9/16 15:45 8
2015/7/23 17:05 15
2015/9/14 12:45 20
2015/6/17 21:44 26
2015/5/13 12:45 28
given time is: 2015/7/23 17:05, two data counts above and below which are more close to give time are"
My desired output:
2015/8/5 12:13 2
2015/6/13 22:13 5
2015/9/14 12:45 20
2015/6/17 21:44 26
many many thanks in advance,
0 件のコメント
回答 (2 件)
Andrei Bobrov
2017 年 9 月 16 日
編集済み: Andrei Bobrov
2017 年 9 月 16 日
T = readtable('20170916.txt','CollectOutput',1);
T.Var1 = strcat(T{:,1}(:,1),{' '},T{:,1}(:,2));
T.
t = datetime('2015/7/23 17:05','f','uuuu/MM/dd HH:mm')
z = abs(day(T{:,1} - t))
t = ~z;
y = ~t.*(cumsum(t) + 1 );
n = numel(z);
kk = (1:n)';
s = false(n,1);
for ii = 1:2
m = y == ii;
p = kk(m);
[~,v] = sort(z(m));
s(p(v(1:2))) = true;
end
out = T(s,:)
out =
4×2 table
Date data
________________ ____
2015/08/05 12:13 2
2015/06/13 22:13 5
2015/09/14 12:45 20
2015/06/17 21:44 26
0 件のコメント
dpb
2017 年 9 月 16 日
編集済み: dpb
2017 年 9 月 16 日
>> dnm=datetime(c(:,1),'format','yyyy/M/d HH:mm'); % convert date strings
>> target=datetime('2015/7/23 17:05','format','yyyy/M/d HH:mm'); % and the target date
>> [dnm,ix]=sort(dnm); % order, keep index
>> idx=[find(dnm<target,2,'last');find(dnm>target,2,'first')] % find closest two either side of target
idx =
2
3
5
6
>> c(ix(idx),:) % look up originals based on presort index
ans =
'2015/6/13 22:13' [ 5]
'2015/6/17 21:44' [26]
'2015/8/5 12:13' [ 2]
'2015/9/14 12:45' [20]
>>
Unlike the previous Answer this replaces, this doesn't assume the target time is identically one of those in the data vector (although that might be always so, this is more generic).
Also the previous note on sort wasn't so; not sure what was wrong that caused that symptom earlier...
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Linear and Nonlinear Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!