Mapping a vector onto a matrix

I have a n*m-matrix A and a n-dimensional vector b. I want to scale the i-th row of my matrix by the i-th coefficient in my vector b. Essentially, I'm trying to achieve this:

A[i, :] = A[i, :] / b[i]

Ideally I'd like to do it without using for loops. Here's my code so far:

let res = self.my_vector
              .map(|vector| {
                      .map(|(a, b)| a.to_owned() / b.sqrt())
              .map_err(|err| err.clone())

Note that my_vector is of type std::result::Result<ArrayBase<OwnedRepr<F>, Dim<[usize; 1]>>>, hence the self.my_vector.as_ref().map(|vector| { ... }) part.

This code outputs std::result::Result<Map<std::iter::Zip<AxisIter<'_, F, Dim<[usize; 1]>>, ndarray::iter::Iter<'_, F, Dim<[usize; 1]>>>, _>, while I'd like res to be std::result::Result<Array2<F>>.

I think I struggle building a 2d array from this axis_iter-map syntax. I tried to add .collect() but I was told that Array2d cannot be built from an iterator over elements of Array1d.

Any help is appreciated
Many thanks in advance

I think you are using ndarray crate to implement 2D and 1D array.
How about using broadcasting functionality and reversed_axes() like below?

use ndarray::array;

fn main(){
    let a = array![[1.0, 2.0], [3.0, 4.0]];
    let b = array![1.0, 2.0];

    println!("{:?}", (a.reversed_axes() / b).reversed_axes());

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.