Dividing daily rainfall data in to monthly data
9 views (last 30 days)
Show older comments
I have a daily rainfall data from 1961 to 1995. I already compiled it to 34X365. Now I want to have monthly data seperately as follows
Jan - 31 days
Feb - 28 days
likewise
December 31 days
Next year January - 31 days like wise
0 Comments
Answers (2)
Dheeraj
on 1 Jul 2024
Hi Sewwandhi Chandrasekara,
I understand you want to decompose your yearly data of daily rainfall data to monthly data.
You can do this by splitting the matrix into separate matrices for each month, considering the number of days in each month. Since your data does not account for leap years, February is always considered to have 28 days.
Here is an example snippet in MATLAB to do the same.
% Assuming your data is stored in a matrix 'rainfallData' of size 34x365
rainfallData = rand(34, 365); % Replace this with your actual data
% Define the number of days in each month
daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
% Initialize a cell array to hold monthly data
monthlyData = cell(34, 12);
% Process each year
for year = 1:34
startIndex = 1;
for month = 1:12
endIndex = startIndex + daysInMonth(month) - 1;
% Extract data for the current month
monthlyData{year, month} = rainfallData(year, startIndex:endIndex);
% Update the start index for the next month
startIndex = endIndex + 1;
end
end
% Now monthlyData{year, month} contains the rainfall data for the specified month
% For example, to get the January data of the first year
janFirstYear = monthlyData{1, 1};
disp('January Data of the first year:');
disp(janFirstYear);
% If you want to concatenate the data for each month across all years:
allYearsMonthlyData = cell(1, 12);
for month = 1:12
allYearsMonthlyData{month} = cell2mat(arrayfun(@(x) monthlyData{x, month}, 1:34, 'UniformOutput', false)');
end
% For example, to get all January data across all years:
allJanData = allYearsMonthlyData{1};
disp('January Data across all years:');
disp(allJanData);
0 Comments
Star Strider
on 3 Jul 2024
Rainfall = rand(ceil((1996-1961)*365.25),1)*10; % Rainfall Data
Date = datetime(1961,1,1) + caldays(0:size(Rainfall,1)-1).'; % Days
TT1 = timetable(Date,Rainfall) % Create Tiemtable
TT1RTSm = retime(TT1, 'monthly', 'sum') % Monthly Sum
TT1RTMd = retime(TT1, 'monthly', 'median') % Monthly Median
If you do not have these, it can be a bit more complicated, however easily done —
dv = datevec(datenum(Date));
[Dy,idx1,idx2] = unique(dv(:,1)+dv(:,2)/11.99,'stable');
YrMoc = accumarray(idx2, (1:numel(idx2)).', [], @(x){unique(dv(x,[1 2]),'rows')});
YrMo = cell2mat(YrMoc);
Year = YrMo(:,1);
Month = YrMo(:,2);
Rain = TT1.Rainfall;
RainCalcc = accumarray(idx2, (1:numel(idx2)).', [], @(x){sum(Rain(x))}); % Data Accumulation Calculation
RainSum = cell2mat(RainCalcc);
MonthlyRainfallSum = table(Year, Month, RainSum)
To get different metric calculations, change the last argument in ‘RainCalcc’ to calculate what you want.
So to calculate the median, that would be:
RainCalcc = accumarray(idx2, (1:numel(idx2)).', [], @(x){median(Rain(x))}); % Data Accumulation Calculation
Make appropriate changes to deal with missing (NaN) values, depending on the options available to you.
.
0 Comments
See Also
Categories
Find more on Data Preprocessing 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!