Extracting data from histogram plots
조회 수: 14 (최근 30일)
이전 댓글 표시
Hello. I'm trying to process some data from some chemical analyses I did a while ago. I have 3 types of data: particle diameter, nitrogen content (%), and sulfur content (%). I've already managed to organize the particle diameter data into a histogram plot with something like 50 bins. Now, I'd like to figure out the average nitrogen and sulfur content of the particles in each bin. I'm not sure how to do this, though, and I haven't found any obvious tutorials to explain how to do this. Any advice?
댓글 수: 0
채택된 답변
Adam Danz
2023년 3월 10일
편집: Adam Danz
2023년 3월 11일
3 methods to group data and compute mean for each group
Each method deals with empty bins differently.
discretize + splitapply
Use discretize to group each value into the bins used in histogram and then splitapply to compute the mean for each group. Note that each bin must contain at least one data point.
Example: compute the mean of data in bins defined by edges.
rng default % for reproducibility of this demo
data = rand(1,100)*100;
edges = 0:10:100;
binID = discretize(data,edges)
a = splitapply(@mean,data,binID)
discretize + groupsummary
Use discretize to group each value into the bins and then groupsummary to compute the mean of each group. When working with vectors, the first two arguments must be column vectors.
Note that the output vector skips empty bins. See additional outputs to groupsummary to identify which bins are represented in the first output.
s = groupsummary(data(:),binID(:),'mean')
discretize + accumarray
Use discretize to group each value into the bins and then accumarray to compute the mean of all bins.
Note that empty bins are represented by a 0.
m = accumarray(binID(:),data,[],@mean)
Comparison of these methods when some bins are empty
data = randn(100,1)+10; % expected range: ~6 : ~13
edges = 0:3:15; % 5 bins but the first two will be empty
binID = discretize(data, edges);
m = accumarray(binID,data,[],@mean)
s = groupsummary(data,binID(:),'mean')
a = splitapply(@mean,data,binID)
댓글 수: 7
Adam Danz
2023년 3월 11일
Let's keep it civil here.
As you mentioned, if one of the bins have no values, then splitapply won't work.
I'll add alternatives to my answer.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!