Skip to main content

digitize_values

Function digitize_values 

Source
pub fn digitize_values<T>(
    array: ArrayView<'_, T, Ix1>,
    bins: ArrayView<'_, T, Ix1>,
    right: bool,
) -> Result<Array<T, Ix1>, &'static str>
where T: Clone + Float + FromPrimitive,
Expand description

Return the bin-edge values corresponding to each element in the input array.

For each input value, this finds which bin it falls in (same logic as digitize) and returns the corresponding bin edge value. Values that fall before the first edge return the first edge; values past the last edge return the last edge.

§Arguments

  • array - Input array
  • bins - Array of bin edges (must be monotonically increasing)
  • right - If true, intervals are (edge, next_edge]; if false, [edge, next_edge)

§Returns

Array of bin-edge values with the same length as array

§Examples

use ::ndarray::array;
use scirs2_core::ndarray_ext::stats::digitize_values;

let data = array![1.2f64, 3.5, 5.1, 0.8, 2.9];
let bins = array![1.0f64, 3.0, 5.0];
let values = digitize_values(data.view(), bins.view(), false).expect("Operation failed");

assert_eq!(values[0], 1.0); // 1.2 falls in bin starting at 1.0
assert_eq!(values[1], 3.0); // 3.5 falls in bin starting at 3.0
assert_eq!(values[2], 5.0); // 5.1 is past all edges, returns last edge
assert_eq!(values[3], 1.0); // 0.8 is before first edge, returns first edge