필터 지우기
필터 지우기

Problem with splitapply (bin averaging)

조회 수: 4 (최근 30일)
Farbod Tabaei
Farbod Tabaei 2022년 7월 7일
댓글: Farbod Tabaei 2022년 7월 11일
I'm having issues with running splitapply
Error using splitapply (line 111)
For N groups, every integer between 1 and N must occur at least once in the vector of group numbers.
Error in Binning_Code_cont (line 198)
binMean_VWC_09 = splitapply(@(vec_VWC_09)mean(vec_VWC_09,'omitnan'),vec_NEP_VWC_09,hbin_VWC_09);
The weird thing is that I use the same code for another variable and it gives me the desired outcome (see attached plot), By looking at this plot you would understand what I'm trying to do here. In the plot below, y-axis values are averaged for every 0.5 interval on the x-axis
% Removing nighttime values (PAR = 0) and VWC values of less than equal to zero
T_VWC_09 = table(VWC_09,NEP_09,PAR_09,'VariableNames', {'VWC','NEP','PAR'});
T_VWC_09_final = rmmissing(T_VWC_09);
T_VWC_09_final(T_VWC_09_final.PAR==0,:)=[];
T_VWC_09_final(T_VWC_09_final.VWC <= 0,:)=[];
vec_VWC_09 = T_VWC_09_final.VWC; % pulling out VWC (x-axis)
vec_NEP_VWC_09 = T_VWC_09_final.NEP; % pulling out NEP (y-axis)
% Calculating measurements of binned VWC (bin size 0.05) against values of NEP
binWidth2 = 0.05;
edges_VWC_09 = min(0) : binWidth2 : max(vec_VWC_09);
[~, ~, hbin_VWC_09] = histcounts(vec_VWC_09,[edges_VWC_09,inf]);
binMean_VWC_09 = splitapply(@(vec_VWC_09)mean(vec_VWC_09,'omitnan'),vec_NEP_VWC_09,hbin_VWC_09);
I have also attached my two variables in csv format for your reference. Thank you in advance!

채택된 답변

Adam Danz
Adam Danz 2022년 7월 7일
The grouping variable in splitapply is expected to be a vector of positive integers 1 to n and cannot contain a missing group value. Here's an example that does not contain a group for 3:
g = [1 1 2 2 4 4];
data = 1:6;
splitapply(@mean,data,g)
It appears that your data is not present in some of the bins computed by histcounts.
A workaround is to use findgroups to create your grouping variable.
[g, gid] = findgroups(hbin_VWC_09)
out = splitapply(@(vec_VWC_09)mean(vec_VWC_09,'omitnan'), vec_NEP_VWC_09, g)
Although out(i) may no longer correspond with bin i if some bins are emtpy. You can address that using gid which defines the bin for each group value.
Another alternative is to use arrayfun instead of splitapply.
  댓글 수: 6
Adam Danz
Adam Danz 2022년 7월 11일
Nice use of grpstats. Thanks for sharing!
Farbod Tabaei
Farbod Tabaei 2022년 7월 11일
Thank you @Adam Danz :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 7월 7일
If I'm correct, if you called histcounts without ignoring the first output argument that output argument would contain a 0, indicating one of the bins has no data.
As a simpler example that demonstrates the problem I think you're experiencing:
try
splitapply(@sum, 1:3, [2 2 3]) % nothing in group 1
catch ME
fprintf("This code threw an error. Error message:\n%s", ME.message)
end
This code threw an error. Error message: For N groups, every integer between 1 and N must occur at least once in the vector of group numbers.
You could try adding a "dummy" value for the empty group(s).
splitapply(@(x) sum(x, 'omitnan'), [1:3 NaN], [2 2 3 1])
ans = 1×3
0 3 3

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by