group by indexing discontinuous timetable

11 vues (au cours des 30 derniers jours)
Eric Escoto
Eric Escoto le 18 Nov 2021
Commenté : Kelly Kearney le 19 Nov 2021
I am tying to identify groupings based on two temporal criteria from a discontinuous timetable array. The timetable is aggregated to 1-minute intervals.
The criteria are:
1. Minimum of five continuous rowtime minutes,
and
2. at least one rowtime less than 15 minutes from #1 (above) or #2).
Put another way, at least five minutes of data and thereafter if one minute is within 15 consecutive minutes continue to include those minutes to the group.
In the end, I want to be able to perform algebraic and other operations on the variable contained in each new grouping (for example taking the sum of each group).
Example data is provided. It looks like there should be a total of 4 groupings. One of the rows (113) is a time that doesn't meet the critera above and thus should either be discarded or flagged differently from the groups so I can remove it later (maybe by a 'nan' flag?).

Réponse acceptée

Kelly Kearney
Kelly Kearney le 19 Nov 2021
Here's one possible solution; it first checks for criteria 2 and then goes back to verify #1. You can probably do it all in one fell swoop but finding consecutive runs can get messy so I prefer to keep that on its own. I also opted not to mark the extra groups with a NaN, because splitapply and other grouping functions insist on consecutive-integer groupings; I find it cleaner to filter those out after the fact.
% First group by the within-15-minutes criteria
dt = minutes(diff(test1.TIMESTAMP));
grp = cumsum([true; dt>15]);
% Now check that each subgroup has at least a 5-minute run of consecutives
maxrun = @(x) max(diff([1; find(minutes(diff(x)) > 1); length(x)+1]));
isgood = splitapply(@(x) maxrun(x)>=5, test1.TIMESTAMP, grp);
% Sum of each group
grpsum = splitapply(@sum, test1.mean_V, grp);
grpsum(~isgood) = NaN;
  3 commentaires
Eric Escoto
Eric Escoto le 19 Nov 2021
Looks like all I need to do to get other variables is modify the 'Var' name in any generic 'TT.Var' format.
Kelly Kearney
Kelly Kearney le 19 Nov 2021
You can use the same splitapply setup to do whatever calculations you need:
nt = splitapply(@length, test1.TIMESTAMP, grp); % number of elements in each
t0 = splitapply(@min, test1.TIMESTAMP, grp); % earliest time in each

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Exponents and Logarithms dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by