Element wise maximum reduce - similar to numpy.maximum.reduce(arr) #1173
Unanswered
bruingineer
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Sure, use ndarray::prelude::*;
use ndarray::Zip;
fn main() {
let a = array![[1, 2, 1, 2], [3, 0, 3, 0]];
assert_eq!(
Zip::from(a.row(0))
.and(a.row(1))
.map_collect(|&a, &b| a.max(b)),
array![3, 2, 3, 2],
);
} If there may be more than two rows, and you want the maximum element among all the rows, use ndarray::prelude::*;
fn main() {
let a = array![[1, 2, 1, 2], [3, 0, 3, 0]];
assert_eq!(
// Note: if axis 0 has length 0, this will return an array
// of all `i32::MIN`.
a.fold_axis(Axis(0), i32::MIN, |&a, &b| a.max(b)),
array![3, 2, 3, 2],
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What would be the best way to make similar functionality to numpy's maximum.reduce feature?
example:
np.maximum.reduce([[1, 2, 1, 2], [3, 0, 3, 0]]
results in
[3, 2, 3, 2]
Can I use zip to compare the elements and return the max?
Beta Was this translation helpful? Give feedback.
All reactions