histc: Histogram Count (Matlab style)

View source: R/histc.R

histcR Documentation

Histogram Count (Matlab style)

Description

Histogram-like counting.

Usage

  histc(x, edges)

Arguments

x

numeric vector or matrix.

edges

numeric vector of grid points, must be monotonically non-decreasing.

Details

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.

If x is a matrix then cnt and bin are matrices too, and

for (j in (1:n)) cnt[k,j] <- sum(bin[, j] == k)

Value

returns a list with components cnt and bin. n(k) counts the number of values in x that lie between edges(k) <= x(i) < edges(k+1). The last counts any values of x that match edges(n). Values outside the values in edges are not counted. Use -Inf and Inf in edges to include all values.

bin[i] returns k if edges(k) <= x(i) < edges(k+1), and 0 if x[i] lies outside the grid.

See Also

hist, histss, findInterval

Examples

x <- seq(0.0, 1.0, by = 0.05)
e <- seq(0.1, 0.9, by = 0.10)
histc(x, e)
# $cnt
# [1] 2 2 2 2 2 2 2 2 1
# $bin
# [1] 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 0 0

## Not run: 
# Compare
findInterval(x, e)
# [1] 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 9
findInterval(x, e, all.inside = TRUE)
# [1] 1 1 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 8 8 8
# cnt[i] <- sum(findInterval(x, e) == i)
## End(Not run)

x <- matrix( c(0.5029, 0.2375, 0.2243, 0.8495,
               0.0532, 0.1644, 0.4215, 0.4135,
               0.7854, 0.0879, 0.1221, 0.6170), 3, 4, byrow = TRUE)
e <- seq(0.0, 1.0, by = 0.2)
histc(x, e)
# $cnt
#      [,1] [,2] [,3] [,4]
# [1,]    1    2    1    0
# [2,]    0    1    1    0
# [3,]    1    0    1    1
# [4,]    1    0    0    1
# [5,]    0    0    0    1
# [6,]    0    0    0    0
# 
# $bin
#      [,1] [,2] [,3] [,4]
# [1,]    3    2    2    5
# [2,]    1    1    3    3
# [3,]    4    1    1    4

pracma documentation built on Nov. 10, 2023, 1:14 a.m.