Remove rows with less than certain amount of measurements from timetable
19 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have a 517x2 timetable that I've reduced based on some conditions shown in my code below. Next, I want to remove any rows with measurements that do not contribute a full day's worth of measurements. A full day contains 8 measurements, so in the photo we see that rows with April 19 and May 6 dates would be removed, but May 7 and 8 would not since they contain 8 measurements per day each. Please let me know if this does not make sense. Any tips are appreciated, this is my first time using timetables / tables. Thank you very much in advance.
%% Hm0 <4m & Tp > 8s
Hm0_OM = Hm0_yy';
Tp_OM = Tp_yy';
dnum_OM = dnum_yy';
% Create table with values
OM_occ = timetable(dnum_OM, Hm0_OM, Tp_OM);
%Remove values based on O&M requirements
OM_occ(OM_occ.Hm0_OM > 1.5,:)=[];
OM_occ(OM_occ.Tp_OM > 8,:)=[];
% Remove rows that do not contribute to a full day's worth of measurements (8 per day)
2 个评论
采纳的回答
Hank
2020-4-29
编辑:Hank
2020-4-29
I have some idea about getting the times and unique times
T = tab.Time
uT = unique(T);
and then checking how many times each unique time appears in T in a for loop. Let me know if it needs tweaking.
rm = zeros(size(T),'logical'); % array tracking rows to be removed
for i=1:length(uT)
A = uT(i)==T; % logical array, size of T
if sum(A)<8 % sum(A) counts the entries with that unique datetime
rm = rm | A; % add the rows to the rm array by logical or.
end
end
T(rm,:) = []; % delete rows that didn't match spec
If we start removing rows within the loop, the loop index will lose sync with the array, hence the rm array.
5 个评论
Hank
2020-5-4
The trick was finding a way to compare the datetime objects that would be true if they're the same date (ignoring the time of day). I think converting to strings was a sloppy way to do this, but it allows us to use the unique function and to count dates that are the same with strcmp.
I'd love to see if someone comes up with a cleaner method.
更多回答(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!