Calculate daily standard deviation from timetable

10 visualizaciones (últimos 30 días)
Ancalagon8
Ancalagon8 el 21 de Dic. de 2022
Editada: Ancalagon8 el 6 de En. de 2025
Hello, I want to calculate the mean max and mean min standard deviation of temperature data from a daily timetable.
How can I calculate the standard deviation for each day?

Respuesta aceptada

Star Strider
Star Strider el 21 de Dic. de 2022
Try something like this —
DT = datetime('01-Dec-2022 00:00:00') + hours(0:720).';
Temperature = sin(2*pi*(0:numel(DT)-1)/24-0.5).'*12+5 + randn(size(DT))*1.5;
TT1 = timetable(DT, Temperature)
TT1 = 721×1 timetable
DT Temperature ____________________ ___________ 01-Dec-2022 00:00:00 -0.6988 01-Dec-2022 01:00:00 1.2667 01-Dec-2022 02:00:00 3.0934 01-Dec-2022 03:00:00 7.2162 01-Dec-2022 04:00:00 10.372 01-Dec-2022 05:00:00 12.655 01-Dec-2022 06:00:00 15.885 01-Dec-2022 07:00:00 18.277 01-Dec-2022 08:00:00 16.86 01-Dec-2022 09:00:00 16.837 01-Dec-2022 10:00:00 19.089 01-Dec-2022 11:00:00 14.336 01-Dec-2022 12:00:00 12.287 01-Dec-2022 13:00:00 9.4703 01-Dec-2022 14:00:00 2.5353 01-Dec-2022 15:00:00 -1.0999
figure
plot(TT1.DT, TT1.Temperature)
grid
DailyMax = retime(TT1, 'daily', 'max');
DailyMin = retime(TT1, 'daily', 'min');
DailySTD = retime(TT1, 'daily', @std);
TT2 = table(DailyMax.DT, DailyMax.Temperature, DailyMin.Temperature, DailySTD.Temperature, 'VariableNames',{'DT','Max','Min','StDv'})
TT2 = 31×4 table
DT Max Min StDv ___________ ______ _______ ______ 01-Dec-2022 19.089 -8.976 9.209 02-Dec-2022 16.877 -7.9704 8.9778 03-Dec-2022 18.891 -8.2106 8.3563 04-Dec-2022 17.053 -7.7505 8.9039 05-Dec-2022 19.937 -8.5247 8.9055 06-Dec-2022 17.545 -9.3624 8.7491 07-Dec-2022 19.835 -6.6562 8.0717 08-Dec-2022 18.21 -8.5766 8.8617 09-Dec-2022 17.416 -6.1877 8.1288 10-Dec-2022 17.298 -8.2496 8.4282 11-Dec-2022 18.21 -6.3769 8.891 12-Dec-2022 19.579 -8.3538 9.035 13-Dec-2022 20.088 -7.3316 8.5688 14-Dec-2022 19.397 -8.5091 8.7861 15-Dec-2022 17.578 -8.363 8.9894 16-Dec-2022 17.249 -8.5435 9.1342
The standard deviation of a single value is zero by definition, so it is only possible to calculate the standard deviation of all temperatures for a single day for that day.
It is possible to calculate the standard deviation for the maximum and minimum temperatures for several days (at least more than 1) and of course for the entire month.
.
  27 comentarios
Star Strider
Star Strider el 30 de En. de 2023
Try something like this —
DT = datetime('01-Dec-2022 00:00:00') + hours(1:364*24).'; % Hours
Temperature = sin(2*pi*(0:numel(DT)-1)/24-0.5).'*12+5 + randn(size(DT))*1.5; % Temperatures
TT1 = timetable(DT, Temperature)
TT1 = 8736×1 timetable
DT Temperature ____________________ ___________ 01-Dec-2022 01:00:00 -0.37909 01-Dec-2022 02:00:00 0.28714 01-Dec-2022 03:00:00 6.4977 01-Dec-2022 04:00:00 7.7289 01-Dec-2022 05:00:00 12.292 01-Dec-2022 06:00:00 15.373 01-Dec-2022 07:00:00 14.676 01-Dec-2022 08:00:00 15.309 01-Dec-2022 09:00:00 15.836 01-Dec-2022 10:00:00 16.44 01-Dec-2022 11:00:00 16.672 01-Dec-2022 12:00:00 14.35 01-Dec-2022 13:00:00 8.6757 01-Dec-2022 14:00:00 9.1514 01-Dec-2022 15:00:00 6.0855 01-Dec-2022 16:00:00 2.4084
DailyMax = retime(TT1, 'daily', 'max'); % Daily Aggregation
DailyMin = retime(TT1, 'daily', 'min'); % Daily Aggregation
DailyMean = retime(TT1, 'daily', 'mean'); % Daily Aggregation
DailyRange = DailyMax.Temperature - DailyMin.Temperature; % Daily Aggregation
DailyTT = timetable(DailyMax.DT, DailyMax.Temperature, DailyMin.Temperature, DailyRange, DailyMean.Temperature, 'VariableNames',{'Max_Temperature','Min_Temperature','Range_Temperature','Mean_Temperature'})
DailyTT = 365×4 timetable
Time Max_Temperature Min_Temperature Range_Temperature Mean_Temperature ___________ _______________ _______________ _________________ ________________ 01-Dec-2022 16.672 -9.4342 26.106 5.1988 02-Dec-2022 18.117 -8.7949 26.911 5.3819 03-Dec-2022 18.611 -7.9121 26.523 5.1666 04-Dec-2022 18.68 -8.1488 26.829 4.8373 05-Dec-2022 17.69 -7.1424 24.832 5.3975 06-Dec-2022 19.069 -8.1585 27.228 5.2481 07-Dec-2022 15.966 -9.9869 25.952 4.8057 08-Dec-2022 18.182 -9.3876 27.57 5.4564 09-Dec-2022 17.444 -8.3602 25.804 4.8884 10-Dec-2022 19.302 -7.5362 26.838 5.391 11-Dec-2022 16.696 -10.497 27.193 4.528 12-Dec-2022 18.3 -6.9914 25.291 5.1051 13-Dec-2022 18.151 -8.5995 26.75 5.4419 14-Dec-2022 17.836 -10.976 28.811 5.0321 15-Dec-2022 17.282 -9.1337 26.416 4.8376 16-Dec-2022 18.325 -8.0825 26.408 4.8119
MonthlyTTMax = retime(DailyTT, 'monthly', 'max')
MonthlyTTMax = 12×4 timetable
Time Max_Temperature Min_Temperature Range_Temperature Mean_Temperature ___________ _______________ _______________ _________________ ________________ 01-Dec-2022 20.326 -6.2942 28.852 5.4564 01-Jan-2023 20.882 -6.5416 30.436 5.486 01-Feb-2023 21.771 -6.7069 29.895 5.42 01-Mar-2023 20.752 -6.3323 30.718 5.7682 01-Apr-2023 19.842 -6.1608 28.69 5.81 01-May-2023 19.771 -6.3754 29.726 5.3571 01-Jun-2023 20.162 -6.2147 29.415 5.839 01-Jul-2023 21.085 -6.1956 30.339 5.8512 01-Aug-2023 20.793 -5.192 29.184 5.6606 01-Sep-2023 20.346 -6.2424 29.367 5.5407 01-Oct-2023 20.827 -5.951 29.933 5.4544 01-Nov-2023 20.722 -5.9304 30.332 5.5216
MonthlyTTMin = retime(DailyTT, 'monthly', 'min')
MonthlyTTMin = 12×4 timetable
Time Max_Temperature Min_Temperature Range_Temperature Mean_Temperature ___________ _______________ _______________ _________________ ________________ 01-Dec-2022 15.966 -10.976 22.838 4.4957 01-Jan-2023 16.409 -10.684 24.377 4.5691 01-Feb-2023 15.547 -10.91 24.217 4.3471 01-Mar-2023 15.748 -10.449 23.368 4.5982 01-Apr-2023 15.941 -10.591 22.533 4.2688 01-May-2023 16.111 -10.972 23.687 4.3221 01-Jun-2023 16.484 -11.179 23.418 4.3588 01-Jul-2023 16.605 -10.795 24.173 4.5544 01-Aug-2023 15.518 -10.379 22.288 4.3416 01-Sep-2023 15.359 -10.131 22.941 4.5646 01-Oct-2023 16.226 -10.036 22.756 4.4768 01-Nov-2023 -5.9304 -10.465 0 -5.9304
MonthlyTTMean = retime(DailyTT, 'monthly', 'mean')
MonthlyTTMean = 12×4 timetable
Time Max_Temperature Min_Temperature Range_Temperature Mean_Temperature ___________ _______________ _______________ _________________ ________________ 01-Dec-2022 17.894 -8.3725 26.267 5.0415 01-Jan-2023 18.351 -8.3832 26.735 4.9523 01-Feb-2023 18.018 -8.3188 26.337 4.923 01-Mar-2023 18.023 -8.1983 26.222 5.0542 01-Apr-2023 17.919 -8.011 25.93 5.0405 01-May-2023 18.123 -8.3906 26.513 4.902 01-Jun-2023 17.953 -8.0177 25.971 5.0389 01-Jul-2023 18.193 -8.1407 26.334 5.0607 01-Aug-2023 18.041 -8.2389 26.28 4.9959 01-Sep-2023 18.051 -7.866 25.917 5.0096 01-Oct-2023 17.892 -7.9565 25.848 4.9462 01-Nov-2023 17.38 -8.1048 25.485 4.6034
MonthlyTTSD = retime(DailyTT, 'monthly', @(x)std(x)) % Monthly Aggregation Of Daily Data
MonthlyTTSD = 12×4 timetable
Time Max_Temperature Min_Temperature Range_Temperature Mean_Temperature ___________ _______________ _______________ _________________ ________________ 01-Dec-2022 0.89012 1.0981 1.2396 0.28508 01-Jan-2023 1.1506 1.0405 1.3665 0.27683 01-Feb-2023 1.1575 1.0735 1.6071 0.29298 01-Mar-2023 1.2434 0.99746 1.8445 0.28534 01-Apr-2023 1 1.1311 1.6295 0.34489 01-May-2023 0.99102 1.1128 1.4308 0.28412 01-Jun-2023 0.8422 1.0872 1.5087 0.3929 01-Jul-2023 0.87463 1.033 1.4316 0.31694 01-Aug-2023 1.0816 1.1013 1.7648 0.32041 01-Sep-2023 1.1947 1.0865 1.6608 0.27119 01-Oct-2023 1.043 0.97543 1.5385 0.24107 01-Nov-2023 4.5398 1.0787 5.0632 2.007
MthV = DT(1) + calmonths(0:11).';
figure
hold on
plot(MthV, MonthlyTTSD{:,1}, '-r')
plot(MthV, MonthlyTTSD{:,2}, '-b')
hold off
legend('Max Monthly SD','Min Monthly SD', 'Location','best')
The ‘MonthlyTT’ set of timetable arrays should have everything you need. If necessary, it should be straightforward to abstract the various variables from them and import them to a new summary timetable, similar to what I did to create ‘DailyTT’.
Creating numbered variables such as:
MeanDailyRange_January=mean(DailyRange_January);
is generally discouraged. You can get the various monthly values from the timetable arrays that calculated them.
I am doing my best to provide what you want, however that is not always clear to me. The result is that we need to iterate until we converge on the correct arrays.
.
Star Strider
Star Strider el 30 de En. de 2023
As always, my pleasure!
I thought we already covered that problem at some point. By now I’ve lost track of where it is (although searching for it, I believe it was here).
It would be essentially the same approach.
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Preprocessing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by