Am I using histc incorrectly or is it a MATLAB error?

So here is the code in MATLAB:

data = [1 1.5 2 3 4 4.5 5 6 7 7 7 0 0 0]; histc(data, [1:1:5]) histc(data, [1:1:5, inf]) histc(data, [-inf, 1:1:5]) 

which outputs the following:

 ans = 2 1 1 2 1 ans = 2 1 1 2 5 0 ans = 3 2 1 1 2 1 

My question is: why does MATLAB return a useless 0 when you use inf in size bin (in this case means> = 5)?

Will it always be zero? Help says that the output will always be the same length as the size of the hopper, but isn't that a bad indicator in this case?

+4
source share
1 answer

This is indeed the correct HISTC behavior. When you use the syntax:

 n = histc(x,edges); 

then from the documentation:

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

Therefore, the last edge value you give returns the number of matches. When inf is the last value of the edge, it counts 0 (i.e. there is no inf in the data). When 5 is the last value of the edge, it exactly matches 1 value in the data.

+7
source

Source: https://habr.com/ru/post/1286150/


All Articles