histc - Histogram count

Syntax

n = histc(x,edges)
n = histc(x,edges,dim)
[n,bin] = histc(...)

Description

n = histc(x,edges) counts the number of values in vector x that fall between the elements in the edges vector (which must contain monotonically nondecreasing values). n is a length(edges) vector containing these counts. No elements of x can be complex.

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). Values outside the values in edges are not counted. Use -inf and inf in edges to include all non-NaN values.

For matrices, histc(x,edges) returns a matrix of column histogram counts. For N-D arrays, histc(x,edges) operates along the first nonsingleton dimension.

n = histc(x,edges,dim) operates along the dimension dim.

[n,bin] = histc(...) also returns an index matrix bin. If x is a vector, n(k) = sum(bin==k). bin is zero for out of range values. If x is an M-by-N matrix, then

for j=1:N, 
n(k,j) = sum(bin(:,j)==k);
end

To plot the histogram, use the bar command.

Examples

Generate a cumulative histogram of a distribution.

Consider the following distribution:

x = -2.9:0.1:2.9;
y = randn(10000,1);
figure(1), hist(y,x)

Calculate number of elements in each bin

n_elements = histc(y,x);

Calculate the cumulative sum of these elements using cumsum

c_elements = cumsum(n_elements)

Plot the cumulative histogram

figure(2),bar(x,c_elements)

See Also

hist, mode

Specialized Plotting for related functions

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS