필터 지우기
필터 지우기

How to compute monthly average and standard deviation with for loop

조회 수: 2 (최근 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 CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by