Problem with splitapply (bin averaging)
Show older comments
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!
Accepted Answer
More Answers (1)
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
You could try adding a "dummy" value for the empty group(s).
splitapply(@(x) sum(x, 'omitnan'), [1:3 NaN], [2 2 3 1])
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!