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;
// }
});
alice
April 22, 2021, 7:31pm
2
Please format your code correctly.
To format code in this forum you need to surround the code with three backticks (```). For example, typing this...
```
fn main() {
println!()
}
```
...will be rendered as highlighted Rust code, like so:
fn main() {
println!()
}
You can also tell the forum software which language you're using by adding the language after the opening backticks.
```cpp
#include <iostream>
int main() {
std::cout << "Hello World!";
}
```
Will appear with highlighting as:
#include <iostream>
int main(…
the code formatting doesn't work.
alice
April 22, 2021, 8:33pm
4
You are using a single-quote, not a backtick or tilde.
1 Like
bluss
April 22, 2021, 9:09pm
5
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
system
Closed
July 21, 2021, 9:16pm
7
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.