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.
回答 (1 件)
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
2020 年 5 月 25 日
Ameer Hamza
2020 年 5 月 25 日
編集済み: Ameer Hamza
2020 年 5 月 25 日
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
Valerio Gianforte
2020 年 5 月 25 日
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];
カテゴリ
ヘルプ センター および File Exchange で Descriptive Statistics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!