Averaging values within bins

Hi,
I have the two arrays x and y (attached), which I plot with plot(x,y). This is creating "bins" of width 5:
What is the fastest way to average the values within each such bin, such that we end up with one value/bin, especially if we don't want to pre-define bin width?
Thanks

댓글 수: 3

Stephen23
Stephen23 2018년 10월 28일
편집: Stephen23 2018년 10월 28일
"This is creating "bins" of width 5:"
Are you referring to the x-tick marks and x-tick labels? Note that the data has not been processed according to those tick marks. They are simply an artifact of the axes.
If you want to bin data, then you will need to do this yourself, e.g. using a histogram function, or accumarray, or something similar.
MiauMiau
MiauMiau 2018년 10월 28일
I don't want to have a histogram in the classical sense: I want the mean value within a bin (not the count). Would the histogram function help nevertheless?
Stephen23
Stephen23 2018년 10월 28일
"Would the histogram function help nevertheless?"
Yes, because they can also return the indices.

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

답변 (2개)

Bruno Luong
Bruno Luong 2018년 10월 28일

2 개 추천

load('y.mat')
load('x.mat')
plot(x,y)
edges = (0:5:50);
[~,~,loc]=histcounts(x,edges);
meany = accumarray(loc(:),y(:))./accumarray(loc(:),1);
xmid = 0.5*(edges(1:end-1)+edges(2:end));
figure
plot(x,y)
hold on;
plot(xmid,meany,'r')

댓글 수: 2

AnnieLieseMari
AnnieLieseMari 2022년 6월 13일
Hi there!
I'm working with a very similar data set and need some help with my code. I know this was posted and answered a while ago, but I would really appreciate the help.
After using the same code you provided, but with my data set and edges of (4.25:0.5:17.75), I am getting an error saying "First input SUBS must contain positive integer subscripts."
I have a data set of x and y values that is upwards of 2000+ data points. It's super big and when I zip it, the Help Center is still saying it's too big. Please let me know if you'd like me to provide a sample of maybe 20 or so data points to help out.
I think the main reason this is happening is that my data has NaN values in it. Though, I'm not sure how to remove those. Of course, if I remove the x values, I have to preserve the y values so the other pairs still match up.
Simply remove the bad data:
keep = isfinite(x) & isfinit(y);
x = x(keep);
y = y(keep);
... do the rest
Make sure the extrem edges cover entirely your data.

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

Steven Lord
Steven Lord 2018년 10월 28일

0 개 추천

Do you want to use histogram or histcounts to bin the data, or are you looking for something more of a moving average, where the value of the averaged signal at a particular time is the mean of the data for that time and some data points before and after that time? If the latter, see movmean.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

질문:

2018년 10월 28일

댓글:

2022년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by