sorry if this is a noob question but is it posible to apply a function to every element in a nalgebra vector?
if I for example have a vector like this:
type Vector2x1 = SVector<f32, 2>;
let vector = Vector2x1::new(2.0, 2.0);
how can I apply a function which takes a f32 as an argument to every element in the vector?
Re-stating @AlphaModder's comment, you can use the function
map
to do this. Alternativaley you could iterate over the elements and map each one, but.map(x)
conveniently handles the other steps for you without the overhead (if any) of an iterator (Ex:.into_iter().map(x).collect::<Self>()
).If you did not already know, you can also use a function in place of the closure.
This approach will work for any variant of
Vector
orMatrix
(impl<T, R: Dim, C: Dim, S: RawStorage<T, R, C>> Matrix<T, R, C, S>
).