'elementwise comparison in ndarray::Array1<f64> in rust
I'm trying to find the rust equivalent of this python numpy code doing elementwise comparison of an array.
import numpy as np
np.arange(3) > 1
Here's my rust code:
use ndarray::Array1;
fn main() {
let arr = Array1::<f64>::range(0.0, 3.0, 1.0);
arr > 1.0 // doesn't work.
}
I could do this with a for loop, but I'm looking for the most idiomatic way of doing it.
Solution 1:[1]
Solution 2:[2]
Use .map for element-wise operations:
fn main() {
let v: Vec<_> = (0..3).map(|x| x > 1).collect();
println!("{v:?}")
}
Output:
[false, false, true]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chad |
| Solution 2 |
