Since the edges
argument has to have monotonically nondecreasing values, one way to flip the edge behavior is to negate and flip the edges
argument and negate the values for binning. If you then flip the bin count output from HISTC, you should see the typical edge behavior of HISTC reversed:
n = fliplr(histc(-x,-fliplr(edges)));
The above uses FLIPLR, so x
and edges
should be row vectors (i.e. 1-by-N). This code will bin data according to the following criteria:
- The first bin
n(1)
counts any values of x
that match edges(1)
.
- The other bins
n(k)
count the values x(i)
such that edges(k-1) < x(i) <= edges(k)
.
Note that this flips the edge behavior of all the bins, not just the first and last bins! The typical behavior of HISTC for bin n(k)
uses the equation edges(k) <= x(i) < edges(k+1)
(Note the difference between the indices and which side has the equals sign!).
EDIT: After some discussion...
If you instead wanted to bin data according to the following criteria:
- The first bin
n(1)
counts any values of x
that match edges(1)
.
- The second bin
n(2)
counts the values x(i)
such that edges(1) < x(i) < edges(2)
.
- The other bins
n(k)
count the values x(i)
such that edges(k-1) <= x(i) < edges(k)
.
Then the following should accomplish this:
n = histc(x,[edges(1) edges(1)+eps(edges(1)) edges(2:end)]);
n(end) = [];
The first bin should capture only values equal to edges(1)
, while the lower edge of the second bin should start at an incremental value above edges(1)
(found using the EPS function). The last bin, which counts the number of values equal to edges(end)
, is thrown out.