How to average data within bins when values are not integers?

4 views (last 30 days)
I'm analyzing whitecaps, named 'wc_average', against wind speed ('new_u10wcav'). I want to bin the windspeeds and average the whitecaps within those bins. I've tried to use accumuarray but I keep getting errors since my data aren't positive integers. The current code I've been trying comes up with the error
Error using splitapply (line 101)
The data variables must have the same number of columns as the vector of group numbers. The group number vector has 11 column(s), and data variable 1 has 1 column(s).
How do I fix it? My current code:
numBins=10;
[N,edges]=histcounts(new_u10wcav,numBins);
meany=splitapply(@mean,wc_average,findgroups(edges));
The data should be founds in testfiles.mat that I attached.

Answers (1)

Matt J
Matt J on 12 Mar 2021
numBins=10;
bins=discretize(new_u10wcav,numBins);
meany=splitapply(@mean,wc_average(:),findgroups(bins(:)));

Community Treasure Hunt

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

Start Hunting!

Translated by