How do I squeeze? #1361
-
I abstracted my ndarray from my user, such that each dimension of my array has a different enum variant. This, does not work: fn get<'a, D>(&'a self, args: &[Argument]) -> ArrayView<'a, String, D::Smaller>
where D: RemoveAxis + Dimension
{
let mut slice = self.array.slice(s![.., .., .., ..]);
for arg in args {
match arg {
Argument::Data(_) => {
let d = self.arg2idx[&arg];
slice = slice.slice(s![d..d+1, .., .., ..]);
},
Argument::Feature(_) => {
let f = self.arg2idx[&arg];
slice = slice.slice(s![.., f..f+1, .., ..]);
},
Argument::Modification(_) => {
let m = self.arg2idx[&arg];
slice = slice.slice(s![.., .., m..m+1, ..]);
},
Argument::FileType(_) => {
let t = self.arg2idx[&arg];
slice = slice.slice(s![.., .., .., t..t+1]);
},
};
}
let out: ArrayView<'a, String, D::Smaller> = slice; // Not Alllowed.
for (axis, size) in slice.shape().iter().enumerate() {
if *size == 1 {
out = out.remove_axis(Axis(axis));
}
}
out
} Now I'm using a weird for-loop that checks for axis with a size of 1, but even that doesn't work since Rust refuses to make my slice be of a dynamic number of axis. My problem would be solved with a numpy-like squeeze function, how do I achieve that? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I don't understand why you use a range instead of an index.
That would give you a 3d array (which seems to be what you want) instead of 4 dimensions with one of the dimension with a length of 1. Am I missing something? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
D
andD::Smaller
statically correspond to a fixed number of axes (in the general case). Take a D-parameterized array as input, but convert to dynamic dimensional (IxDyn) first thing you do in your function, only then is it possible to return something with a dynamic number of axes.