Closed
Description
I asked this question on reddit, but I didn't get any response over there. I'm currently using ndarray for a project of mine and one thing I need to quite frequently is to mutate the contents of two or more columns/rows based on each others contents. It isn't always the case that both lanes need to be mutable, but they usually need to be. I've figured out how to do it with ndarray::Zip
and iter_axis_mut
as follows
pub fn apply_two_lanes<T, F>(
arr: &mut Array2<T>,
axis: Axis,
l1: usize,
l2: usize,
f: F,
) where
F: Fn(&mut T, &mut T),
{
assert_ne!(l1, l2);
let mut lanes = arr.axis_iter_mut(axis);
let (lane1, lane2) = if l1 < l2 {
(lanes.nth(l1).unwrap(), lanes.nth(l2 - l1 - 1).unwrap())
} else {
let (lane2, lane1) =
(lanes.nth(l2).unwrap(), lanes.nth(l1 - l2 - 1).unwrap());
(lane1, lane2)
};
ndarray::Zip::from(lane1).and(lane2).apply(f);
}
However, as you can see, because I don't know if l1
or l2
comes first, I need the additional if test. I haven't managed to figure out how to do the above without some manner of checking of the ordering of l1
and l2
. Is there a way to accomplish the above without having to check which index is first?
Metadata
Metadata
Assignees
Labels
No labels