rustrust-ndarray

How do you get the maximum value from a rust ndarray?


Unlike NumPy ndarrays in Python, the arrays provided by the rust ndarray crate do not seem to have max method (or min, argmax or argmin) for that matter. What is the best way to get the maximum value from such an array (assuming it can be f32 and f64 rather than just integer types)?

use ndarray::Array

pub fn main() {
    let x = Array::from(vec![-3.4, 1.2, 8.9, 2.0]);

    // get max?
}

Solution

  • The ndarray-stats crate provides the QuantileExt traits for ndarray's, which provides a max method (and min, argmax and argmin), e.g.,

    use ndarray::Array
    use ndarray_stats::QuantileExt
    
    pub fn main() {
        let x = Array::from(vec![-3.4, 1.2, 8.9, 2.0]);
    
        // get max
        let maxvalue = x.max().unwrap();
    
        println!("The maximum value is {}", maxvalue);
    }