Problem with splitapply (bin averaging)
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Farbod Tabaei
el 7 de Jul. de 2022
Comentada: Farbod Tabaei
el 11 de Jul. de 2022
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!
0 comentarios
Respuesta aceptada
Adam Danz
el 7 de Jul. de 2022
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.
[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.
6 comentarios
Más respuestas (1)
Steven Lord
el 7 de Jul. de 2022
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])
0 comentarios
Ver también
Categorías
Más información sobre Environment and Settings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!