bincount

Count weighted number of occurrences of each value in slice of non-negative ints. Note that empty weight causes compiler error.

  1. auto bincount(T xs, size_t minlength)
  2. auto bincount(T xs, W weights, size_t minlength)
    pure nothrow @safe
    bincount
    (
    T
    W
    )
    (
    T xs
    ,,
    size_t minlength = 0
    )
    if (
    isSlice!W
    )

Parameters

xs T

input slice

weights W

weights slice of the same length as xs

minlength size_t

a minimum number of bins for the output array

Return Value

Type: auto

slice like weights of weighted number of ocurrences TODO: support @nogc

Examples

import numir : bincount;
import mir.ndslice : sliced, iota, fuse;

auto ys = [0, 1, 1, 0, 1].sliced!size_t;
assert(ys.bincount == [2, 3]);
assert(ys.bincount(iota(ys.length)) == [0+3, 1+2+4]);
assert(ys.bincount([[1, 0], [-1, 0], [-1, 0], [1, 0], [-1, 0]].fuse) == [[2, 0], [-3,0]]);
assert([].sliced!size_t.bincount == [].sliced!size_t);
assert([].sliced!size_t.bincount([].sliced!double) == [].sliced!double);

Meta