How to get average of data using different time range?

28 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Bjorn Gustavsson
Bjorn Gustavsson il 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 Commenti
Hye-mi Jeong
Hye-mi Jeong il 14 Nov 2022
Hello,
Thanks for your answer.
I applied the code below referring to your code.
Thank you for your ideas.
Temp = randi([-20 40], 24*365, 1); % 365 days data corresponding for 1 hours
T_obs = hours((0:std_time:std_time*24*365)'); % std_time = 1.0356h
% Start_t, End_t are already exist.
Start_T_lims = (Start_t)/3600;
End_T_lims = (End_t)/3600;
T_sum = zeros(numel(T_lims), 1);
time_diff = End_T_lims-Start_T_lims;
for n = 1:(numel(T_lims)-1)
start_idx = find(T_obs(:, 1)<Start_T_lims(n), 1, 'last')+1;
end_idx = find(T_obs(:, 1)>=End_T_lims(n), 1, 'first')-1;
%
start_rem = T_obs(start_idx)-Start_T_lims(n);
end_rem = End_T_lims(n, 1)-T_obs(end_idx);
%
if start_idx > end_idx
T_avg(n, 1) = T_obs(end_idx, 2);
else
T_avg(n, 1) = (sum(Temp(start_idx:end_idx-1))*hours(std_time)+(start_rem.*Temp(start_idx-1))+(end_rem.*Temp(end_idx)))/time_diff(n);
end
end
Bjorn Gustavsson
Bjorn Gustavsson il 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Time Series Events in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by