How to determine average and standard deviation of y axis values for corresponding bin of x axis?
Show older comments
Sir i have two data series say
a b
25.36737061 -27.47956892
20.54391479 -23.68162398
16.76391602 -16.65254461
9.47177124 -19.20600915
16.25158691 -18.56570783
4.462646484 -14.39363913
7.785919189 -14.98048449
12.27481079 -18.49125231
4.851806641 -19.91135093
2.111236572 -5.049334665
-1.457702637 -6.51219601
1.85055542 -1.299793246
and i want to plot for bin width of 'a' (say 5,10,15 etc) with the corresponding average and standard deviation of b. Please help me with a easy way since the series are very large?
5 Comments
trailokya
on 16 Sep 2015
Kelly Kearney
on 16 Sep 2015
histc will return 0 for the idx value of any points that don't fall within your bins (i.e. <0 or >50). You could filter those points out, or change the idx value for those points:
[~,idx] = histc(a, 0:5:50);
idx(idx == 0) = max(idx)+1;
bavg = accumarray(idx, b, [], @mean);
Note that bavg(1:end-2) now corresponds to the averages within the bins, bavg(end-1) is the average of values exactly equal to your rightmost bin edge (50), and bavg(end) is the average of all values either <0 or >50. I usually prefer to make sure that my bins fully encompass the data values... easier to analyze that way.
Image Analyst
on 16 Sep 2015
The average of the bins is not the average of the original data. Be sure you know what you really want and compute it correctly.
Star Strider
on 16 Sep 2015
I explained this in my Answer to your previous post (that you have as yet to Accept).
Kelly Kearney
on 16 Sep 2015
To simplify the tracking of indices, you may want to look at this aggregatehist.m function... it's basically a wrapper around the histc plus accumarray method demonstrated by Star Strider and myself.
ab = [...
25.36737061 -27.47956892
20.54391479 -23.68162398
16.76391602 -16.65254461
9.47177124 -19.20600915
16.25158691 -18.56570783
4.462646484 -14.39363913
7.785919189 -14.98048449
12.27481079 -18.49125231
4.851806641 -19.91135093
2.111236572 -5.049334665
-1.457702637 -6.51219601
1.85055542 -1.299793246];
bin = -5:5:30;
[aa,bb] = aggregatehist(bin, ab(:,1), ab(:,2));
dev = cellfun(@std, bb);
avg = cellfun(@mean, bb);
amid = (bin(1:end-1)+bin(2:end))./2;
plot(ab(:,1), ab(:,2), '.');
hold on;
errorbar(amid, avg, dev, 'linestyle', 'none', 'marker', 'o');
set(gca, 'xtick', bin, 'xgrid', 'on');

Accepted Answer
More Answers (0)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!