How to compute monthly average and standard deviation with for loop

Hi everyone,
I have to compute the monthly average and standard deviation on each year. I tryed to do this one but I obtain the error 'The logical indices contain a true value outside of the array bounds.' and I don't know how to fix it.
format long g
folder = 'D:\Valerio\data\IPCC_midcent\RCP4.5\BCC_CSM\BCC_CSM.xlsx';
file = xlsread(folder);
start_yy = 2026;
end_yy = 2044;
range_yy = (start_yy:end_yy).';
r_yy = length(range_yy);
dt = datetime([file(:,1:3) file(:,4)/1E4 repmat([0 0],size(file,1),1)]);
tt = timetable(dt, file(:,5:end));
data = tt.Var1;
a = datenum({'01-Jan-2026','31-Dec-2026'});
aa = datevec(a(1):1:a(2));
Out = aa(:,1:3);
mm = Out(:,2);
for i = 1:r_yy
s1 = sprintf('01-Jan-%d',2025+i);
s2 = sprintf('01-Jan-%d',2026+i);
TR = timerange(s1,s2);
tt_TR = tt(TR,:);
data_TR = tt_TR.Var1;
Hs = data_TR(:,1);
Tp = data_TR(:,2);
for j = 1:12
mm_Hs(j,i) = mean(Hs(mm == j));
mm_Tp(j,i) = mean(Tp(mm == j));
dev_Hs(j,i) = std(Hs(mm == j));
dev_Tp(j,i) = std(Tp(mm == j));
end
end
The error is at the first iteration of the second for loop. Is there someone that can help me. Thank you so much.

Answers (1)

If you have Statistics and Machine Learning Toolbox, then you can use grpstat(). For example
format long g
folder = 'BCC_CSM.xlsx';
T = readtable(folder);
T(:,2:4) = []; % delete month day and hour columns as they are not important for yearly mean
T.Properties.VariableNames = {'year', 'data1', 'data2', 'data3'};
result = grpstats(T, 'year', {'mean', 'std'});
result_mean = grpstats(T, 'year', 'mean');
result_std = grpstats(T, 'year', 'std');
Otherwise you can use splitapply()
data = xlsread('BCC_CSM.xlsx');
data(:, 2:4) = []; % delete month day and hour columns as they are not important for yearly mean
[grps, years] = findgroups(data(:,1));
result_mean = splitapply(@mean, data(:,2:end), grps);
result_mean = [years result_mean];
result_std = splitapply(@std, data(:,2:end), grps);
result_std = [years result_std];

4 Comments

Thank you for your answer but I need of monthly mean and monthly standard deviation of each year. If I've understood fine with your method I can compute only the yearly mean and yearly standard deviation.
Ok. do you want to calculate monthly mean and std for all years or mean for each year separately? In other words, there is data for 19 years, how many rows do you expect in the final table? 12 or 12*19=228
I want to compute monthly mean and std for all years and so I expect a final table 12x19
In that case, try following codes.
grpstat()
format long g
folder = 'BCC_CSM.xlsx';
T = readtable(folder);
T(:,3:4) = []; % delete month day and hour columns as they are not important for yearly mean
T.Properties.VariableNames = {'year', 'months', 'data1', 'data2', 'data3'};
result = grpstats(T, {'year', 'months'}, {'mean', 'std'});
result_mean = grpstats(T, {'year', 'months'}, 'mean');
result_std = grpstats(T, {'year', 'months'}, 'std');
splitapply()
data = xlsread('BCC_CSM.xlsx');
data(:, 3:4) = []; % delete month day and hour columns as they are not important for yearly mean
[grps, Years, Months] = findgroups(data(:,1), data(:,2));
result_mean = splitapply(@(x) mean(x, 1), data(:,3:end), grps);
result_mean = [Years Months result_mean];
result_std = splitapply(@(x) std(x, [], 1), data(:,3:end), grps);
result_std = [Years Months result_std];

Sign in to comment.

Products

Release

R2018b

Asked:

on 25 May 2020

Commented:

on 25 May 2020

Community Treasure Hunt

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

Start Hunting!