convert a range vector of bin centers to bin edges. Bin Centers and edges are non-uniform

19 views (last 30 days)
Is there a trivial way of converting a vector that contain bin centers to bin edges. The caveat here is that they are unequal. Does matlab provide a trivial way to do it?
Thanks

Answers (3)

Image Analyst
Image Analyst on 22 Oct 2013
Where do you want the edges? Exactly half-way between the centers? If so you simply use conv():
% Make up some non-uniformly spaced bin centers.
binCenters = unique(sort(randi(99, 1,10)))
% Get edges half way in between.
binEdges = conv(binCenters, [0.5, 0.5], 'valid')
% Define end points outside of bin centers.
binEdges = [0 binEdges inf]

W. Owen Brimijoin
W. Owen Brimijoin on 22 Oct 2013
If we assume the simplest case, that the edges should be precisely between the centres (i.e., not geometric, or logarithmic, etc), and that the first and last edge should be -inf and +inf, respectively, then this is a simple solution:
%example of random bin centres:
centers = cumsum(randi(10,20,1))
%create equidistant edges:
edges = [-inf;centers(:)+[diff(centers(:))/2;inf]];

tsan toso
tsan toso on 23 Oct 2013
Hi all
Thanks for the all the feedback.
I am working with centers that are not evenly spaced.
Correct me if I am wrong the formulas provided would on occasion not give me edges that are equidistant from the left and right of center bin. That is to say the bins would no longer be the center of the edges anymore.
Would there be way such that the edges are identified such that the centers would always be the center of each and every edge?
Thanks
  3 Comments
Image Analyst
Image Analyst on 23 Oct 2013
I agree with Owen. The formula make the edges exactly half way between the original centers, but the distance from the new edges to the original centers will be different on each side of the original center, unless you have uniformly spaced centers.
tsan toso
tsan toso on 23 Oct 2013
Thanks Owen and Image.
Bummer. I was just trying to use the kmeans function to bin my 1d data, but it returns the centers instead and I need it in edges. Dividing the distance between centres seem to inaccurately define the edge between clusters.
Guess have to work with what I have.
Cheers

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!