How can I estimate the average for each one of the intervals in an x axis?

12 ビュー (過去 30 日間)
Hi
I need a method to split my x axis into intervals and estimate the average for each one of the intervals. The plots are polynomial curves, if that helps.
Any suggestion?
Thank you
  2 件のコメント
Jan
Jan 2019 年 5 月 17 日
The question is not clear yet: It does not make sense to "split the x axis". But if you have two vectors, one containing the x and the other the y values of a curve, you can calculate a blockwise average. So please post, what your inputs are.
Stelios Fanourakis
Stelios Fanourakis 2019 年 5 月 19 日
Exactly as you stated. Two vectors of X,Y in a 2d plot. I want to define intervals for the X values and average each interval and all together.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 5 月 19 日
編集済み: Adam Danz 2019 年 5 月 20 日
This is relatively straight forward with histcounts() and splitapply().
% Create fake data
x = randi(1000,1,1000); %doesn't have to be sorted
y = rand(size(x));
% Define the edges of your x data
% There's lots of ways to do this - this is just 1 example.
binWidth = 100; %how wide is each bin?
edges = min(x) : binWidth : max(x);
% determine which bin each x element is in
[~, ~, hbin] = histcounts(x,[edges,inf]);
% Get mean of y within each bin
% Here I ignore NaN values, too.
binMean = splitapply(@(x)mean(x,'omitna'),y,hbin);
To interpret the results, binMean(n) is the mean for all y values where x is greater or equal to edges(n) and less than edges(n+1).
To plot the data, the bins, and display the average values per bin above the axis:
figure
plot(x,y,'b.')
hold on
plot([edges;edges],[0,1],'k')
text(edges+binWidth/2, ones(size(edges)), strsplit(num2str(binMean,.1)), ...
'VerticalAlignment', 'Bottom', 'HorizontalAlignment', 'Center')
% * binMean must be a row vector
  13 件のコメント
Adam Danz
Adam Danz 2019 年 5 月 20 日
編集済み: Adam Danz 2019 年 5 月 20 日
Glad I could help.
The 2nd input to text() indicates where along the y axis your labels should go. In my example I simply used y=1 for all labels because my data didn't span beyond 1. You could just do this
ylim([-0.5, 2])
text(edges+binWidth/2, ones(size(edges))*max(ylim()), ......
also, see the "rotation" text property. It might look better to rotate the labels by 90 deg.
Also, to draw the lines across your entire axis,
plot([edges;edges],[min(ylim()), max(ylim())],'k')
Stelios Fanourakis
Stelios Fanourakis 2019 年 5 月 20 日
Thanks once again for your additional help. Althought, I might keep labels horizontally as they appear 'cause it looks better to me, than rotating them 90 degrees and have a vertical appearance.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by