How to index an entire 1d array from a 2d array? #1214
Answered
by
jturner314
TrevorSatori
asked this question in
Q&A
-
Sorry if this is noobie, but I have been stuck on this for hours. In the docs it makes very clear how to index a specific value from a 2d array arr[[0,1]] = 5, but how would I replace a 1d array (row) inside a 2d array? I believe it should look something like this. let mut x: Array2 = Array::zeros((10 , 5)); x[0] += [0,0,0,0,0]; please help. |
Beta Was this translation helpful? Give feedback.
Answered by
jturner314
Oct 1, 2022
Replies: 1 comment 1 reply
-
I'd suggest reading the Slicing and Subviews docs. The Here's some sample code for your specific question: use ndarray::prelude::*;
use std::ops::AddAssign;
fn main() {
let mut x: Array2<i32> = Array::zeros((10, 5));
// The examples below use `.row_mut()`. Other options include
// `.slice_mut()` and `.index_axis_mut()`.
// Assign a row.
x.row_mut(0).assign(&aview1(&[0,0,0,0,0]));
// Fill all elements of a row with the same value.
x.row_mut(0).fill(0);
// Add to a row in-place (+= operation). Note the `use std::ops::AddAssign` above.
x.row_mut(0).add_assign(&aview1(&[0,0,0,0,0]));
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
TrevorSatori
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd suggest reading the Slicing and Subviews docs. The
ndarray
for NumPy users guide may also be of interest.Here's some sample code for your specific question: