Summing elements in an array between uneven date intervals

1 次查看(过去 30 天)
I have a dataset of daily rainfall. I also have a list of 28 days on which environmental sampling was carried out. I'm trying to figure out how to sum the total precipitation that fell between each environmental sampling (represented by "sample_dates")
The environmental samplings are mostly about 30 days apart, but not exactly. I already know how to find the monthly precipitation, but what I want is the exact amount of rainfall that fell between each sampling.
Any advice on what functions to use would be so appreciated. I've read documentation for cumsum, ismember, movsum, and a bunch of previously asked questions but I can't find anything that's helped me come up with a solution.
T = readtable("precip_test.csv", "VariableNamingRule","preserve");
A = table2array(T);
%sequential dates from Sept 2019 to Dec 2022
rain_dates = A(:,1);
%daily precipitation in mm for each day
rain_mm = A(:,2);
%dates that environmental samples were taken
sample_dates = A(:,3);
sample_dates(any(isnan(sample_dates), 2), :) = [];

采纳的回答

Star Strider
Star Strider 2023-10-6
编辑:Star Strider 2023-10-6
I am not certain what you want. If I am readiing the file correctly and converting the dates correctly (both 'excel' and 'posixtime' give reasonable results for the dates, however the ‘Date Time’ and ‘Sample Dates’ do not make sense with 'excel') the ‘Sample Dates’ seem to give appropriate results with ‘Precip (mm)’ so it would seem that with that we are finished and nothing further need be done.
T = readtable("precip_test.csv", "VariableNamingRule","preserve")
T = 1338×3 table
Date Precip (mm) Sample dates _____ ___________ ____________ 43709 6.7 43773 43710 0 43802 43711 0 43838 43712 0.4 43865 43713 15.9 43899 43714 5 43927 43715 4.8 43959 43716 2 43984 43717 0.1 43984 43718 0 44018 43719 0 44075 43720 0 44102 43721 0 44144 43722 0.4 44166 43723 4.6 44204 43724 12.8 44235
DateTime = datetime(T.Date, 'ConvertFrom','excel');
SampleDates = datetime(T.('Sample dates'), 'ConvertFrom','excel');
T2 = table(DateTime, T.('Precip (mm)'), SampleDates, 'VariableNames',{'Date Time','Precip (mm)','Sample Dates'})
T2 = 1338×3 table
Date Time Precip (mm) Sample Dates ___________ ___________ ____________ 01-Sep-2019 6.7 04-Nov-2019 02-Sep-2019 0 03-Dec-2019 03-Sep-2019 0 08-Jan-2020 04-Sep-2019 0.4 04-Feb-2020 05-Sep-2019 15.9 09-Mar-2020 06-Sep-2019 5 06-Apr-2020 07-Sep-2019 4.8 08-May-2020 08-Sep-2019 2 02-Jun-2020 09-Sep-2019 0.1 02-Jun-2020 10-Sep-2019 0 06-Jul-2020 11-Sep-2019 0 01-Sep-2020 12-Sep-2019 0 28-Sep-2020 13-Sep-2019 0 09-Nov-2020 14-Sep-2019 0.4 01-Dec-2020 15-Sep-2019 4.6 08-Jan-2021 16-Sep-2019 12.8 08-Feb-2021
T2 = rmmissing(T2); % Remove 'NaT' Rows
LastLine = T2(end,:)
LastLine = 1×3 table
Date Time Precip (mm) Sample Dates ___________ ___________ ____________ 28-Sep-2019 0 07-Jun-2022
VN = T2.Properties.VariableNames;
figure
stairs(T2.('Date Time'), T2.('Precip (mm)'))
grid
xlabel(VN{3})
ylabel(VN{2})
return % Stop Here
A = table2array(T);
%sequential dates from Sept 2019 to Dec 2022
rain_dates = A(:,1);
%daily precipitation in mm for each day
rain_mm = A(:,2);
%dates that environmental samples were taken
sample_dates = A(:,3);
sample_dates(any(isnan(sample_dates), 2), :) = [];
What else do you want to do with these resullts?
.
  15 个评论
Emily
Emily 2023-10-7
thank you! this makes a lot of sense and after reading through the documentation and looking at the examples I feel like i actually understand it, which is awesome. much appreciated :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by