binning data in equally spaced intervals

조회 수: 154 (최근 30일)
shobhit mehrotra
shobhit mehrotra 2015년 3월 11일
댓글: Rahul Kesarkar 2021년 4월 7일
I would like to bin the data in 8 equally spaced bins. I have a the data accessible in vectors [X= conc, Y= alt] this what I have so far to create the bins
binedge = linspace(min(alt),max(alt),6)
then I want to take the average alt and conc of each each bin and plot it. Thanks
  댓글 수: 4
per isakson
per isakson 2015년 3월 11일
The description and examples of the documentation histc, Histogram bin counts (not recommended; use histcounts) are better than mine.
histcounts was introduced in R2014b. I've never used it.
shobhit mehrotra
shobhit mehrotra 2015년 3월 11일
How would i take the average of each bin?

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

답변 (2개)

Josh Meyer
Josh Meyer 2015년 3월 27일
As others noted, histcounts was introduced in R2014b and provides a great deal more flexibility for problems like this.
To use 8 bins, just do:
% Assume the first column is X, second column is Y
data = [rand(100,1), rand(100,1)];
% Find the bin placement based on the X values
[N,edges,bins] = histcounts(data(:,1),8);
The third output, bins, describes the bin placement of each element. So this makes finding the average X and Y value in each bin simple.
for n = 1:8
bin_means(:,n) = mean(data(bins==n,:))';
end
bin_means =
0.0751 0.1979 0.3342 0.4658 0.5691 0.7113 0.8691 0.9676
0.5100 0.6264 0.4949 0.5172 0.5323 0.5556 0.4381 0.6514
  댓글 수: 3
Andrew Hurford
Andrew Hurford 2020년 8월 6일
This worked well for me - but for 1 result in a bin, mean defaults to selecting data across the data, so set the direction flag to 1
Rahul Kesarkar
Rahul Kesarkar 2021년 4월 7일
How would it work if i have to bin data for a particular date range?
For example range : - weekday 12/06/2017 20:00 to 13/06/2017 08:00 and repeat this for every weekday. Weekend 9/06/2017 20:00 to 12/06/2017 08:00. Sum up all the values in the bin.

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


Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2015년 3월 11일
You can use histc to get bin the data, and then accumarray to get the mean of all of the values in each bin:
>> X = 0.25:0.5:5
X =
0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75
>> [B,idx] = histc(X,0:5);
>> V = accumarray(idx(:),X,[],@mean)
V =
0.5000
1.5000
2.5000
3.5000
4.5000
If you have a newer version, then you should use histcounts instead:
[B,~,idx] = histcounts(...);
  댓글 수: 2
shobhit mehrotra
shobhit mehrotra 2015년 3월 11일
I also want the X-value average for each bin, in addition to the y value. Thanks
Stephen23
Stephen23 2015년 3월 11일
편집: Stephen23 2015년 3월 11일
You can simply repeat the accumarray call for each of your X and Y data vectors:
meanofXvalues = accumarray(idx(:),Xvalues,[],@mean)
meanofYvalues = accumarray(idx(:),Yvalues,[],@mean)

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

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by