counts

Purpose

Counts the numbers of elements of a vector that fall into specified ranges.

Format

c = counts(x, v)
Parameters:
  • x (Nx1 vector) – the numbers to be counted

  • v (Px1 vector) – breakpoints specifying the ranges within which counts are to be made. The vector v MUST be sorted in ascending order.

Returns:

c (Px1 vector) –

the counts of the elements of x that fall into the regions:

\[\begin{split}x \leq v[1],\\ v[1] < x \leq v[2],\\ \vdots\\ v[p-1] < x \leq v[p]\end{split}\]

Examples

Basic example

Count the number of elements which are in a specific range.

// Original data
x = { 1.5, 3, 5, 4, 1, 3 };

// Break points
v = { 0, 2, 4 };

// Get counts
c = counts(x, v);
    1.5
    3       0       0
x = 5   v = 2   c = 2
    4       4       3
    1
    3

Count integers

Count how many times each integer from 1 to 10 is present in a vector.

x = { 9, 8, 9, 9, 6, 8, 6, 7 };

ints = seqa(1, 1, 10);

c = counts(x, ints);
       1      0
       2      0
       3      0
       4      0
ints = 5  c = 0
       6      2
       7      1
       8      2
       9      3
      10      0

Remarks

If the maximum value of x is greater than the last element (the maximum value) of v, the sum of the elements of the result, c, will be less than \(N\), the total number of elements in x.

If

    1
    2
    3
    4      4
x = 5  v = 5
    6      8
    7
    8
    9

then

    4
c = 1
    3

The first category can be a missing value if you need to count missings directly. Also \(+\infty\) or \(-\infty\) are allowed as breakpoints. The missing value must be the first breakpoint if it is included as a breakpoint and infinities must be in the proper location depending on their sign. \(-\infty\) must be in the \([2, 1]\) element of the breakpoint vector if there is a missing value as a category as well, otherwise it has to be in the \([1, 1]\) element. If \(+\infty\) is included, it must be the last element of the breakpoint vector.