count

arrayfire.count(array: Array, /, *, axis: None = None, keys: None = None) int | float | complex
arrayfire.count(array: Array, /, *, axis: int, keys: None = None) Array
arrayfire.count(array: Array, /, *, axis: None, keys: Array) tuple[Array, Array]
arrayfire.count(array: Array, /, *, axis: int, keys: Array) tuple[Array, Array]

Count the number of non-zero elements in an ArrayFire array along a specified dimension or across the entire array. Optionally, perform counting based on unique keys.

Parameters

arrayArray

The input multi-dimensional ArrayFire array whose non-zero elements are to be counted.

axisint, optional, keyword-only

The dimension along which the non-zero elements are counted. If None, the total number of non-zero elements across the entire array is returned.

keysArray, optional, keyword-only

An optional one-dimensional ArrayFire array with keys for counting non-zero elements according to unique keys. If provided, axis determines the dimension along which elements are counted per key. If axis is None, it defaults to counting across all dimensions for each key.

Returns

int | float | complex | Array | tuple[Array, Array]
  • If keys is None and axis is None, returns a scalar (int, float, or complex) representing the total count of non-zero elements in array.

  • If keys is None and axis is specified, returns an ArrayFire array representing the count of non-zero elements along the specified axis.

  • If keys is provided, returns a tuple containing two ArrayFire arrays: the unique keys and their corresponding counts. The counts are performed along the specified axis (or across all dimensions if axis is None).

Examples

>>> import arrayfire as af
>>> a = af.randu((3, 3))
>>> a
[3 3 1 1]
    0.6010     0.2126     0.2864
    0.0278     0.0655     0.3410
    0.9806     0.5497     0.7509
>>> b = a > 0.5
>>> b
[3 3 1 1]
    1          0          0
    0          0          0
    1          1          1
>>> af.count(b)
4.0
>>> af.count(b, axis=0)
[1 3 1 1]
    2          1          1