How to compute monthly average and standard deviation with for loop

1 回表示 (過去 30 日間)
Valerio Gianforte
Valerio Gianforte 2020 年 5 月 25 日
コメント済み: Ameer Hamza 2020 年 5 月 25 日
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.

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 25 日
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 件のコメント
Valerio Gianforte
Valerio Gianforte 2020 年 5 月 25 日
I want to compute monthly mean and std for all years and so I expect a final table 12x19
Ameer Hamza
Ameer Hamza 2020 年 5 月 25 日
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];

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Preprocessing についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by