How to get average of data using different time range?
26 views (last 30 days)
Show older comments
Hye-mi Jeong
on 11 Nov 2022
Commented: Bjorn Gustavsson
on 14 Nov 2022
Hello,
I have to get average data with different time range.
'Temp_table' data is not linear, but step data.
For exapmle, like this :
Temperature = randi([-20 40], 24*365, 1); % 365 days data corresponding for 1 hours
Temp_table = timetable(Temperature, 'TimeStep', hours);
And, I have range of time with different interval :
T(1, :) = [seconds(1), seconds(82190)]; % start, end point of time
T(2, :) = [seconds(82191), seconds(101972)];
...
For example, I have to get average T(1) temperature.
I have tried this, but as the time to calculate increases, the calculation speed seems to slow down.
Time_ranges = arrayfun(@(t1, t2) timerange(t1, t2, 'closed'), T(:, 1), T(:, 2), 'un', 0);
Temp_avg = cellfun(@(x) mean(Temp_table(x, 'Temp').Variables), Time_ranges);
Is there a simpler or easier way?
Thanks in advance.
0 Comments
Accepted Answer
Bjorn Gustavsson
on 11 Nov 2022
QD-suggestion (from someone too lazy/stupid/old/stubborn to learn to use arrayfun):
Temperature = randi([-20 40], 24*365, 1); % 365 days data corresponding for 1 hours
T_obs = -0.5 + (1:24*365); % Assumed time of measurement-intervall, centred
% mocking up time-limits:
dT = 37*rand(47,1);
T_lims = cumsum([0,dT']); % just to get something with obviously variable range between limits
for i_lims = (numel(T_lims)-1):-1:1
Temp_avg(i_lims) = mean(Temperature(T_lims(i_lims)<T_obs&T_obs<=T_lims(i_lims+1)));
n_elems(i_lims) = sum((T_lims(i_lims)<T_obs&T_obs<=T_lims(i_lims+1)));
end
HTH
2 Comments
Bjorn Gustavsson
on 14 Nov 2022
My pleasure. Happy that it helped.
Depending on how irregular you have your data and what data is missing, and the additional analysis you want to do, this might be a very "ungraceful" work - If you want something like average temperature or temperature-variation for the 10-11 LT time-periods you'll have to extract those samples, and if you have jittering of the sample-time this will be another tedious step, if you want to look for things like seasonal variations of this you might have biases seeping into these estimates from irregular distribution of gaps in those samples and on and on. Here wishing you strength and perserverance!
More Answers (0)
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!