How to access element in a zipped 1d viewed array, rather than the viewedrepr itself

In the code below, when i try and compare counter_i to 3 i get an error: binary operation > cannot be applied to type ndarray::ArrayBase<ndarray::ViewRepr<&usize>, ndarray::Dim<[usize; 0]>>rustc(E0369)

Why can't i access the elements of the array? Thanks

let mut sample_duration_results = Array2::<f64>::zeros((50,2));
let counter = Array::from((0_usize..50_usize).collect::<Vec<usize>>());
sample_duration_results.axis_iter_mut(Axis(0)).into_par_iter().zip(counter.outer_iter().into_par_iter()).for_each(|(mut results_i,counter_i)| {
    // println!("{:?}",counter_i);
    // let this = counter_i[0];
    if counter_i > 3 {
        //do something
    }
});

One way is to use .iter() or .into_par_iter() instead of .outer_iter():

sample_duration_results.axis_iter_mut(Axis(0)).into_par_iter().zip(counter.into_par_iter()).for_each(|(mut results_i,counter_i)| {
    if *counter_i > 3 {
        //do something
    }
});

Or, if you do have an ArrayView0, you can use .into_scalar() to get the element it contains:

sample_duration_results.axis_iter_mut(Axis(0)).into_par_iter().zip(counter.outer_iter().into_par_iter()).for_each(|(mut results_i,counter_i)| {
    if *counter_i.into_scalar() > 3 {
        //do something
    }
});
2 Likes

You are the greatest @mbrubeck, that was killing me. Into_scalar() wins. Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.