How to flip the definition of edges(1) and edges(end) for the histc function in MATLAB?

孤者浪人 提交于 2019-12-14 03:59:44

问题


In MATLAB:

n = histc(x,edges);

is defined to behave as follows:

n(k) counts the value x(i) if edges(k) <= x(i) < edges(k+1). The last bin counts any values of x that match edges(end).

Is there any way to flip the end behavior such that n(1) counts any values of x that match edges(1), and n(end) counts the values x(i) that satisfy edges(end-1) <= x(i) < edges(end)?


回答1:


Consider the following code:

n = histc(x, [edges(1) edges]);
n(1) = sum(x==edges(1));
n(end) = [];

According to the question posted, the above will return:

  • n(1): counts any values of x that match edges(1)
  • n(k) [k~=1]: counts the value x(i) if edges(k-1) <= x(i) < edges(k)

This different from gnovice solution in that his answer uses the bounds: edges(k-1) < x(i) <= edges(k) (note the position of the equality sign).


To demonstrate, consider this simple example:

x = [0 1 1.5 2 2.5 4 6.5 8 10];
edges = 0:2:10;

>> n = fliplr(histc(-x,-fliplr(edges)))
n =
     1     3     2     0     2     1

corresponding to the intervals: 0 (0,2] (2,4] (4,6] (6,8] (8,10]

Against:

>> n = histc(x, [edges(1) edges]);
>> n(1) = sum(x==edges(1));
>> n(end) = []
n =
     1     3     2     1     1     1

corresponding to the intervals: 0 [0,2) [2,4) [4,6) [6,8) [8,10)




回答2:


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.



来源:https://stackoverflow.com/questions/1557425/how-to-flip-the-definition-of-edges1-and-edgesend-for-the-histc-function-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!