How to get average of data using different time range?

7 views (last 30 days)
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.

Accepted Answer

Bjorn Gustavsson
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)));
Bjorn Gustavsson
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!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by