Assignment to single dim or 0-dim ndarray

if i have a 1-d ndarray, how do i assign values to it?


let mut results = Array1::<f64>::zeros((10));
results.iter_mut().enumerate().for_each(|(index, mut _row)| {
        _row = &mut 3_f64;
        println!("{:?}",_row);
        // if index < length-1 {
        //     let first = index*big_in_small_terms;
        //     let last = first+big_in_small_terms;
        //     _row = &mut vec_of_closes[first..last].last().copied().unwrap();
        //     close = vec_of_closes[first..last].last().copied().unwrap();
        // }
        // else if index == length-1 && remainder > 0 {
        //     let first = index*big_in_small_terms;
        //     let last = first+remainder;
        //     _row = &mut vec_of_closes[first..last].last().copied().unwrap();
        // }
        // else if index == length-1 && remainder == 0 {
        //     _row = &mut close;
        // }
    });

Please format your code correctly.

the code formatting doesn't work.

You are using a single-quote, not a backtick or tilde.

1 Like

The array is 1-dimensional so it's just a glorified Vec at this point (and .iter_mut() would work the same way as with a Vec). Since you've named your iteration element "_row" I'd almost be suspecting you want a two-dimensional array (a.k.a matrix)?

If you do want to assign a single element, use the * operator:

let mut results = Array1::<f64>::zeros((10));
results.iter_mut().enumerate().for_each(|(index, elem)| {
    *elem = 3_f64
});

Your original code mutated the local variable _row rather than the thing it references. To assign to the value it references, use * to dereference it.

1 Like

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.