Dividing daily rainfall data in to monthly data

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

Risposte (2)

Dheeraj
Dheeraj il 1 Lug 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);

Star Strider
Star Strider il 3 Lug 2024
Use a timetable and the retime function, if you have them —
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
TT1 = 12784x1 timetable
Date Rainfall ___________ ________ 01-Jan-1961 7.1762 02-Jan-1961 9.9667 03-Jan-1961 4.5472 04-Jan-1961 1.7033 05-Jan-1961 4.6002 06-Jan-1961 9.431 07-Jan-1961 1.4497 08-Jan-1961 2.0176 09-Jan-1961 2.1423 10-Jan-1961 6.5651 11-Jan-1961 8.6485 12-Jan-1961 0.94793 13-Jan-1961 5.696 14-Jan-1961 3.9544 15-Jan-1961 1.6993 16-Jan-1961 9.2457
TT1RTSm = retime(TT1, 'monthly', 'sum') % Monthly Sum
TT1RTSm = 421x1 timetable
Date Rainfall ___________ ________ 01-Jan-1961 152.02 01-Feb-1961 132.72 01-Mar-1961 169.58 01-Apr-1961 165.57 01-May-1961 133.4 01-Jun-1961 108.55 01-Jul-1961 148.85 01-Aug-1961 171.87 01-Sep-1961 163.46 01-Oct-1961 162.2 01-Nov-1961 151.84 01-Dec-1961 160.84 01-Jan-1962 164.02 01-Feb-1962 144.31 01-Mar-1962 160.18 01-Apr-1962 160.82
TT1RTMd = retime(TT1, 'monthly', 'median') % Monthly Median
TT1RTMd = 421x1 timetable
Date Rainfall ___________ ________ 01-Jan-1961 4.6002 01-Feb-1961 4.504 01-Mar-1961 5.6234 01-Apr-1961 5.9531 01-May-1961 4.3811 01-Jun-1961 3.4975 01-Jul-1961 4.9455 01-Aug-1961 5.8185 01-Sep-1961 5.8105 01-Oct-1961 6.0738 01-Nov-1961 4.7167 01-Dec-1961 5.4816 01-Jan-1962 5.1813 01-Feb-1962 5.3274 01-Mar-1962 5.9308 01-Apr-1962 5.8571
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)
MonthlyRainfallSum = 421x3 table
Year Month RainSum ____ _____ _______ 1961 1 152.02 1961 2 132.72 1961 3 169.58 1961 4 165.57 1961 5 133.4 1961 6 108.55 1961 7 148.85 1961 8 171.87 1961 9 163.46 1961 10 162.2 1961 11 151.84 1961 12 160.84 1962 1 164.02 1962 2 144.31 1962 3 160.18 1962 4 160.82
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.
.

Categorie

Scopri di più su Data Preprocessing 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